Skip to content

Instantly share code, notes, and snippets.

@ahartzog
Created September 21, 2021 00:27
Show Gist options
  • Save ahartzog/c9d6fdd9db257bae0e8b973f65683817 to your computer and use it in GitHub Desktop.
Save ahartzog/c9d6fdd9db257bae0e8b973f65683817 to your computer and use it in GitHub Desktop.
Weekly Cache Clearing Script for React Native
import FastImage from "react-native-fast-image";
import AsyncStorage from '@react-native-community/async-storage';
import moment from 'moment';
import ImagePicker from 'react-native-image-crop-picker';
import Sentry from 'modules/sentry';
const CACHE_TIME_KEY = 'cacheTimestamp'
const timedClearCaches = async () => {
const clearedTimestamp = await AsyncStorage.getItem(CACHE_TIME_KEY);
if( clearedTimestamp === null ){
// Initialize
await AsyncStorage.setItem(CACHE_TIME_KEY, new Date().toISOString());
return;
}
if( clearedTimestamp && moment(clearedTimestamp).isValid() && moment(clearedTimestamp).isBefore(moment().subtract(7, 'days')) ){
Sentry.addBreadcrumb({message: "Clearing app caches"});
// CLEAR ZE CACHES
try{
FastImage.clearDiskCache();
FastImage.clearMemoryCache();
ImagePicker.clean();
await AsyncStorage.setItem(CACHE_TIME_KEY, new Date().toISOString());
}catch(e){
console.devLog.error('Error clearing caches:', e);
Sentry.captureException(e);
}
}
}
export default timedClearCaches;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment