How I can set expiration date for AsyncStorage - react native

Viewed 9846

I am using react native async storage it works good but in some cases, I have to set an expiration date for data and refresh my storage I checked

AsyncStorage documentation but there are no options to set expire after a specific time.

only available options are:-

AsyncStorage.removeItem 
3 Answers

You can use this also, improvement from Ahmed Farag Mostafa answers

import AsyncStorage from "@react-native-async-storage/async-storage";

export default class ExpireStorage {
  static async getItem(key) {
    let data = await AsyncStorage.getItem(key);
    data = JSON.parse(data);
    if (
      data !== null &&
      data.expireAt &&
      new Date(data.expireAt) < new Date()
    ) {
      await AsyncStorage.removeItem(key);
      data = null;
    }
    return data?.value;
  }

  static async setItem(key, value, expireInMinutes) {
    const data = { value };
    if (expireInMinutes) {
      const expireAt = this.getExpireDate(expireInMinutes);
      data.expireAt = expireAt;
    } else {
      const expireAt = JSON.parse(await AsyncStorage.getItem(key))?.expireAt;
      if (expireAt) {
        data.expireAt = expireAt;
      } else {
        return;
      }
    }
    const objectToStore = JSON.stringify(data);
    return AsyncStorage.setItem(key, objectToStore);
  }

  static async removeItem(key) {
    return AsyncStorage.removeItem(key);
  }

  static getExpireDate(expireInMinutes) {
    const now = new Date();
    const expireTime = new Date(now);
    expireTime.setMinutes(now.getMinutes() + expireInMinutes);
    return expireTime;
  }
}

Related