import { loadService } from '@hile/core'
import redisService from '@hile/ioredis'
import { Cache, defineCache, RedisCache } from '@hile/cache'
const userProfileCache = defineCache(
'user:{id:string}:profile',
async ({ id }) => {
const profile = await loadProfileFromDatabase(id)
if (!profile) return new Cache(undefined)
return new Cache(profile).setExpire(300)
},
{
singleflight: { ttl: 10_000, wait: 10_000 },
stale: { ttl: 60 },
negative: { ttl: 30 },
tags: (params, data) => data ? [`user:${params.id}`] : [],
},
)
const redis = await loadService(redisService)
const cache = new RedisCache('app:', redis)
const userProfiles = await cache.loadCache(userProfileCache)
const profile = await userProfiles.read({ id: 'u1' })
await userProfiles.remove({ id: 'u1' })
await cache.removeTag('user:u1')