sunnylqm / react-native-storage

local storage wrapper for both react-native and browser. Support size controlling, auto expiring, remote data auto syncing and getting batch data in one query.
MIT License
3.02k stars 268 forks source link

Way to remove cache? #182

Closed abhionlyone closed 5 years ago

abhionlyone commented 6 years ago

I've set enableCache: true and this is causing few issues in the app.

When the user is logged out, I'm removing all the user data with the below code:

storage.remove({
  key: 'user'
});

But the problem is If I immediately sign in with a new user account, The old user account data is loaded due to enableCache: true.

When I set enableCache: false everything works fine.

So, I'm wondering if there is an option to manually expire cache when a user does log out.

sunnylqm commented 6 years ago

Cache should not have any issue. Can you post your code here?

abhionlyone commented 6 years ago
var storage = new Storage({
    size: 1000,
    storageBackend: AsyncStorage,
    defaultExpires: null,
    enableCache: true,
    sync: {}
})

and I'm saving the user data with the below code:

storage.save({
  key: 'user',
  data: res.data,
  expires: null
 });

and on user logout:

storage.remove({
  key: 'user'
});
abhionlyone commented 6 years ago

The thing is if you reload the app, it will load the new user data. So I assume on reloading the cache is getting expired.

sunnylqm commented 6 years ago

Cache is just in memory and should not affect any logic. What is special in your scenario?

abhionlyone commented 6 years ago

@sunnylqm Ok Thanks!! When I get some time, I will create a simple application and try to replicate the scenario.

Meanwhile, I've set enableCache: false and everything seems to be working clean & nice.

1111mp commented 5 years ago

@sunnylqm 我也遇到了同样的问题 在开发环境下 我的react-native的版本是0.55.4 react-native-storage的版本是0.2.3 我使用的场景是这样的,在app登录成功之后跳转页面并调用相关借口获取数据并存到缓存中: ` export const storage = new Storage({ size: 1000, storageBackend: AsyncStorage, defaultExpires: 1000 3600 24, enableCache: false }) // 全局变量 global.storage = storage;

storage.sync = ASYNC;

在退出app时调用如下的方法清空所有key-id的缓存: clearLogout = () => { LOGOUT().then(res => { storage.clearMap(); this.props.rootStore.init(); this.props.navigation.reset([NavigationActions.navigate({ routeName: 'LoginWrapper' })], 0) },err => {

  })
}

` 但是当我退出登录跳转到登录界面,在不重启app的情况下直接再登录,这时候app的缓存是上一个的缓存,但是当我重启app之后就正常了 然后我设置enableCache: false的时候就不会出现这个情况 求解中。。。

sunnylqm commented 5 years ago

@1111mp Try new 0.2.4 or 1.0.0-beta.1 version.

@AbhiOnlyOne remove is an async promise so you need to await it or use then to ensure the operation orders

abhionlyone commented 5 years ago

@sunnylqm Thanks!

1111mp commented 5 years ago

@sunnylqm Thank you. I'll try it.

1111mp commented 5 years ago

@sunnylqm I just tried it,but the problems still exist in 1.0.0-beta.1 versions. I must to set enableCache: false and the problem can only be solved. That's how I read the data: image

sunnylqm commented 5 years ago

How does this loadAddrBook related to removing data?

1111mp commented 5 years ago

Yes, they don't matter. I logged out after clearing the cache, and then logged in.If I don't set it up like this,the read cache is still the last cache.When I restarted App, the cache returned to normal. I don't know why.

sunnylqm commented 5 years ago

And why do you save data like {key: 'addrBook', id: 'addrBook'}? This should be a key only data and should be removed manually.

sunnylqm commented 5 years ago

https://github.com/sunnylqm/react-native-storage/issues/157#issuecomment-394611729