el-use-cache
v1.0.2
Published
======================================================== AUTHOR: Elias Lopes DATE: 03/06/2026 ========================================================
Readme
======================================================== AUTHOR: Elias Lopes DATE: 03/06/2026
Brief: This is a npm package that was made because when I create my web apps I always need to cache my data to avoid too many database or server requests. I know people can use the localStorgae but they still have to handle deleting from the localStorage after a certain time, or they just forget entirely. So I made this package to make this much easier so people dont have to sit and rewrite the complex logic to cache and decache data in localStorage.
======================================================= WEB HOOK & METHODS
WEB HOOK: useCache() METHODS: { setCachedData, getCachedData }
// default expire constant const DEFAULT_EXPIRE_TIME:number = 60 * 60 * 1000;
// type export type cacheItemType = {data: any, expire: number};
// method headers function getCachedData(k:string): cacheItemType | null {...} function setCachedData(key:string, data:any, expire:number=DEFAULT_EXPIRE_TIME):void {...}
// example of use (with useState) import { useState, useEffect } from 'react'; import useCache from 'el-use-cache';
function UserList() { const [users, setUsers] = useState([]); const { getCachedData, setCachedData } = useCache(); const CACHE_KEY = 'my-users';
useEffect(() => { // Try cache first const cached = getCachedData(CACHE_KEY); if (cached?.data) { setUsers(cached.data); return; } // Otherwise fetch and cache (e.g. 5 minutes) fetch('/api/users') .then((res) => res.json()) .then((data) => { setUsers(data); setCachedData(CACHE_KEY, data, 5 * 60 * 1000); }); }, []);
return ( {users.map((u) => ( {u.name} ))} ); }
