Skip to content

Commit 5b3a532

Browse files
authored
Merge pull request #60 from ethereumfollowprotocol/cache-register
cache-register
2 parents 00170f0 + 3f3c282 commit 5b3a532

File tree

7 files changed

+43
-11
lines changed

7 files changed

+43
-11
lines changed

environment.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
interface EnvironmentVariables {
2+
readonly ALLOW_TTL_MOD: 'true' | 'false'
23
readonly NODE_ENV: 'development' | 'production' | 'test'
34
readonly ENVIRONMENT: 'development' | 'production' | 'stage' | 'test'
45
readonly PORT: string

src/router/api/v1/lists/buttonState/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export function buttonState(lists: Hono<{ Bindings: Environment }>, services: Se
2929
const efp: IEFPIndexerService = services.efp(env(context))
3030
const state: FollowStateResponse = await efp.getListFollowingState(token_id, address)
3131
const packagedResponse = { token_id, address, state }
32-
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse))
32+
if (env(context).ALLOW_TTL_MOD === 'true') {
33+
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse), 0)
34+
} else {
35+
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse))
36+
}
3337
return context.json(packagedResponse, 200)
3438
})
3539
}

src/router/api/v1/lists/followerState/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export function followerState(lists: Hono<{ Bindings: Environment }>, services:
2929
const efp: IEFPIndexerService = services.efp(env(context))
3030
const state: FollowStateResponse = await efp.getListFollowerState(token_id, address)
3131
const packagedResponse = { token_id, address, state }
32-
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse))
32+
if (env(context).ALLOW_TTL_MOD === 'true') {
33+
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse), 0)
34+
} else {
35+
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse))
36+
}
3337
return context.json(packagedResponse, 200)
3438
})
3539
}

src/router/api/v1/lists/stats/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export function stats(lists: Hono<{ Bindings: Environment }>, services: Services
2727
following_count: await efp.getUserFollowingCountByList(token_id)
2828
}
2929

30-
await cacheService.put(cacheTarget, JSON.stringify(stats))
30+
if (env(context).ALLOW_TTL_MOD === 'true') {
31+
await cacheService.put(cacheTarget, JSON.stringify(stats), 0)
32+
} else {
33+
await cacheService.put(cacheTarget, JSON.stringify(stats))
34+
}
3135
return context.json(stats, 200)
3236
})
3337
}

src/router/api/v1/users/followerState/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export function followerState(users: Hono<{ Bindings: Environment }>, services:
3030
const efp: IEFPIndexerService = services.efp(env(context))
3131
const state: FollowStateResponse = await efp.getUserFollowerState(addressUser, addressFollower)
3232
const packagedResponse = { addressUser, addressFollower, state }
33-
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse))
33+
if (env(context).ALLOW_TTL_MOD === 'true') {
34+
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse), 0)
35+
} else {
36+
await cacheService.put(cacheTarget, JSON.stringify(packagedResponse))
37+
}
3438
return context.json(packagedResponse, 200)
3539
})
3640
}

src/router/api/v1/users/stats/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function stats(users: Hono<{ Bindings: Environment }>, services: Services
1313

1414
const cacheService = services.cache(env(context))
1515
const cacheTarget = `users/${addressOrENS}/stats`
16-
if (cache !== 'fresh' || live !== 'true') {
16+
if (cache !== 'fresh') {
1717
const cacheHit = await cacheService.get(cacheTarget)
1818
if (cacheHit) {
1919
return context.json({ ...cacheHit }, 200)
@@ -28,7 +28,16 @@ export function stats(users: Hono<{ Bindings: Environment }>, services: Services
2828
}
2929
}
3030
const efp: IEFPIndexerService = services.efp(env(context))
31+
if (env(context).ALLOW_TTL_MOD === 'true') {
32+
const stats = {
33+
followers_count: await efp.getUserFollowersCount(address),
34+
following_count: await efp.getUserFollowingCount(address)
35+
}
3136

37+
await cacheService.put(cacheTarget, JSON.stringify(stats), 0)
38+
return context.json(stats, 200)
39+
}
40+
console.log(env(context).ALLOW_TTL_MOD)
3241
const ranksAndCounts = await efp.getUserRanksCounts(address)
3342
const stats = {
3443
followers_count: ranksAndCounts.followers,

src/service/cache/service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Environment } from '#/types/index'
55

66
export interface ICacheService {
77
get(key: string): Promise<{} | null>
8-
put(key: string, value: string): Promise<void>
8+
put(key: string, value: string, ttl?: number): Promise<void>
99
}
1010

1111
export class CacheService implements ICacheService {
@@ -52,16 +52,22 @@ export class CacheService implements ICacheService {
5252
return this.#env.EFP_DATA_CACHE.get(key, 'json')
5353
}
5454

55-
async put(key: string, value: string): Promise<void> {
55+
async put(key: string, value: string, ttl = this.#env.CACHE_TTL): Promise<void> {
5656
if (this.#cacheType === 'redis') {
5757
if (!this.#client) {
5858
this.#client = this.createRedisClient()
5959
}
60-
await (this.#client as RedisClientType).set(key, value, {
61-
EX: this.#env.CACHE_TTL
62-
} as any)
60+
if (ttl === 0) {
61+
await (this.#client as RedisClientType).set(key, value, {} as any)
62+
} else {
63+
await (this.#client as RedisClientType).set(key, value, {
64+
EX: ttl
65+
} as any)
66+
}
67+
} else if (ttl === 0) {
68+
await this.#env.EFP_DATA_CACHE.put(key, value, {})
6369
} else {
64-
await this.#env.EFP_DATA_CACHE.put(key, value, { expirationTtl: this.#env.CACHE_TTL })
70+
await this.#env.EFP_DATA_CACHE.put(key, value, { expirationTtl: ttl })
6571
}
6672
}
6773
}

0 commit comments

Comments
 (0)