Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

cache blogposts for 24 hours react native

const cacheIntervaInHours = 24
const cacheExpiryTime = new Date()
cacheExpiryTime.setHours(cacheExpiryTime.getHours() + cacheIntervalInHours)
const lastRequest = await AsyncStorage.getItem("lastRequest")

if (lastRequest == null || lastRequest > cacheExpiryTime) {
        fetch(`${apiUrl}/blogPosts/recent`)
        .then(async (response) => {
            return await response.json()
        })
        .then(async (json) => {
             if (!json || json.length == 0) {
                 throw new Error()
             }
            AsyncStorage.setItem("lastRequest", new Date());
            return await AsyncStorage.setItem('blogPosts', JSON.stringify(json))
        })
        .catch(error => {
            console.error(error)
        })
}
Source by justacoding.blog #
 
PREVIOUS NEXT
Tagged: #cache #blogposts #hours #react #native
ADD COMMENT
Topic
Name
5+2 =