Memory Extended Cache Driver
In-memory storage with sliding expiration: TTL resets on each access, keeping frequently used data alive.
When to Use
Section titled “When to Use”- You want cache entries to stay alive as long as they’re being used
- Session-like or frequently accessed data
Best For
Section titled “Best For”- User sessions
- Frequently accessed, but not permanent, data
Limitations
Section titled “Limitations”- Data is lost on restart
- Not shared between processes or servers
- No eviction policy (see LRU Memory Cache Driver)
Configuration
Section titled “Configuration”import { MemoryExtendedCacheDriver } from "@warlock.js/cache";
const cacheConfigurations = { drivers: { memoryExtended: MemoryExtendedCacheDriver, }, default: "memoryExtended", options: { memoryExtended: { globalPrefix: "sessions", ttl: 600, // 10 minutes }, },};Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
globalPrefix | string | Function | undefined | Global prefix for all cache keys |
ttl | number | Infinity | Default TTL in seconds |
Global Prefix
Section titled “Global Prefix”{ globalPrefix: "sessions", // Static prefix // OR globalPrefix: () => `app-${environment()}`, // Dynamic prefix}TTL Configuration
Section titled “TTL Configuration”{ ttl: 600, // 10 minutes // OR ttl: 3600, // 1 hour // OR ttl: Infinity, // Never expire (default)}Example Usage
Section titled “Example Usage”Storing and Retrieving Data
Section titled “Storing and Retrieving Data”import { cache } from "@warlock.js/cache";
await cache.set("users.42.profile", { userId: 42, name: "Alice" }, CACHE_FOR.HALF_HOUR);const profile = await cache.get("users.42.profile"); // TTL resets to 30 minutesSliding Expiration Explained
Section titled “Sliding Expiration Explained”Unlike the regular Memory Cache Driver, the Memory Extended Driver implements sliding expiration:
- Initial Set: When you set a value with TTL, it’s stored with an expiration time
- Access Resets TTL: Every time you access the value, the TTL is reset to the original duration
- Continuous Access: As long as the value is accessed within the TTL window, it never expires
- Inactivity Expiration: Only when the value is not accessed for the full TTL duration does it expire
Example Timeline
Section titled “Example Timeline”import { cache, CACHE_FOR } from "@warlock.js/cache";
// TTL: 30 minutesawait cache.set("cart.session-xyz", cartData, CACHE_FOR.HALF_HOUR);
// After 15 minutesawait cache.get("cart.session-xyz"); // TTL resets to 30 minutes
// After another 20 minutes (35 minutes total)await cache.get("cart.session-xyz"); // TTL resets to 30 minutes again
// If not accessed for 30 minutes, it expiresUse Cases
Section titled “Use Cases”- User Sessions: Keep sessions alive as long as users are active
- API Rate Limiting: Reset counters on each request
- Frequently Accessed Data: Keep hot data in cache longer
- Temporary State: Maintain state that should persist during activity
Troubleshooting
Section titled “Troubleshooting”- Data disappears after restart: This is expected. Use File or Redis for persistence.
- Cache not shared between processes: Use Redis for distributed cache.
- Items expiring too quickly: Increase the
ttlvalue or access them more frequently.
✅ Supported (brute force). Inherits the memory driver’s similarity behavior — O(N) cosine scan, dev-only at scale.