yarkovaleksei / vue2-storage

Wrapper over browser storage for JavaScript or Vue.js app

Home Page:http://yarkovaleksei.github.io/vue2-storage/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow use both Local and Session

jariesdev opened this issue · comments

On my current application I want to use this plugin but I used Session and Local Storage. Session for current user session and local for longer storage. How can it be done?

does ttl: 0 expires at browser closes?
how can do no expiry value like localstorage does?

To use 2 different repositories, Session and Local, you need to create 2 different Vue instances.
Or, before calling the set method, call the setOptions method, where you specify the driver (session or local) parameter you want.
No other way ((

import Vue from 'vue'
import Vue2Storage from 'vue2-storage'

Vue.use(Vue2Storage, {
  prefix: 'app_',
  driver: 'local', // set local
  ttl: 60 * 60 * 24 * 365 * 1000 // set 1 year ttl
})

new Vue({
  el: '#app',
  created () {
    this.$storage.setOptions({
      driver: 'session', // set session
    })
    this.$storage.set('test', { key: 'value' }, { ttl: 60 * 1000 }) // write to session storage
    this.$storage.setOptions({
      driver: 'local', // set local
    })
  }
}).$mount('#app')

By default, ttl is always 24 hours (60 * 60 * 24 * 1000 // 24 hours). You cannot cancel it in the current version, but you can specify a larger value, for example, 1 year (60 * 60 * 24 * 365 * 1000 // 1 year).

Time ttl when closing the browser does not expire.

Alright I will give it a shoot. I going to have a function that switch over local or session storage. I assume that the previous key/value pairs will remain intact after switching.

anyway, does ttl also applies and required for sessions?

Unfortunately in the current version I didn’t foresee disabling ttl for good.
I will consider this issue and will definitely do it soon.

Thanks for the feedback ))