Skip to content

Commit f07b911

Browse files
committed
add lru cache
1 parent 7190180 commit f07b911

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

config/server.ts

+3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ export const SERVER_CONFIG = {
2323
plausibleServerUrl: process.env.PLAUSIBLE_SERVER_URL, // build time, Docker
2424
// paths
2525
databaseFilePath: join(projectRootFolder, './data/database/', databaseFileName),
26+
// unused
2627
cacheHttpFilePath: join(projectRootFolder, './data/cache/', 'cache-http.json'),
2728
cacheDatabaseFilePath: join(projectRootFolder, './data/cache/', 'cache-database.json'),
2829
logFilePath: join(projectRootFolder, './data/logs/', 'app.html'),
2930
// cache
31+
cacheDatabaseLruItems: 100,
32+
cacheHttpLruItems: 10,
3033
cacheHttpTtlMinutes: 5, // one old-many call
3134
// parser
3235
oldMonthsCount: 12, // one year

docs/working-notes/notes4.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ case insensitive company name consensys
1111
click up api
1212

1313
https://play.clickhouse.com/play?user=play#U0VMRUNUIG1heCh0aW1lKSBGUk9NIGhhY2tlcm5ld3NfaGlzdG9yeQ==
14-
lru cache
14+
lru cache
1515
lighthouse in ssr section readme
1616
need to restart container after cron new month, maybe cache or database volume
1717
jeste do cache, nema sa cache disabled
18+
cache invalidate fails?
1819
mozda samo portainer container recreate fails
1920
----
20-
x new ads since yesterday, both debugging and ui, compare last month updatedAt
21+
N new ads since yesterday, both debugging and ui, compare last month updatedAt // dobar feature
22+
deleteLastNMonths for test updating
23+
remove plausible proxy for country, open issue and ask

libs/keyv.ts

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import fs from 'fs';
22

3-
import Keyv from 'keyv';
3+
import Keyv, { KeyvStoreAdapter } from 'keyv';
44
import KeyvFile from 'keyv-file';
55

66
import { Singleton } from '@/utils/singleton';
77
import { SERVER_CONFIG } from '@/config/server';
88

9-
const { cacheHttpFilePath, cacheDatabaseFilePath, cacheDatabaseDisabled } = SERVER_CONFIG;
9+
const { KeyvLruManagedTtl } = require('keyv-lru');
10+
11+
const {
12+
cacheHttpFilePath,
13+
cacheDatabaseFilePath,
14+
cacheDatabaseLruItems,
15+
cacheHttpLruItems,
16+
cacheDatabaseDisabled,
17+
} = SERVER_CONFIG;
1018

1119
// deletes cache for testing
1220
try {
@@ -15,11 +23,14 @@ try {
1523
} catch (error) {}
1624

1725
class CacheDatabaseInstance {
26+
private static storeAdapter: KeyvStoreAdapter | null = null;
27+
1828
private static createCacheDatabase(): Keyv {
19-
// ttl: undefined - infinite duration
20-
return new Keyv({
21-
store: new KeyvFile({ filename: cacheDatabaseFilePath }),
22-
});
29+
return new Keyv({ store: CacheDatabaseInstance.storeAdapter });
30+
}
31+
32+
public static setAdapter(adapter: KeyvStoreAdapter): void {
33+
CacheDatabaseInstance.storeAdapter = adapter;
2334
}
2435

2536
public static getCacheDatabase(): Keyv {
@@ -29,12 +40,23 @@ class CacheDatabaseInstance {
2940
}
3041
}
3142

43+
// ttl: undefined - infinite duration
44+
const _databaseFileAdapter = new KeyvFile({ filename: cacheDatabaseFilePath });
45+
46+
const databaseLruAdapter = new KeyvLruManagedTtl({ max: cacheDatabaseLruItems });
47+
CacheDatabaseInstance.setAdapter(databaseLruAdapter);
48+
3249
export const getCacheDatabase = CacheDatabaseInstance.getCacheDatabase;
50+
3351
class CacheHttpInstance {
52+
private static storeAdapter: KeyvStoreAdapter | null = null;
53+
3454
private static createCacheHttp(): Keyv {
35-
return new Keyv({
36-
store: new KeyvFile({ filename: cacheHttpFilePath }),
37-
});
55+
return new Keyv({ store: CacheHttpInstance.storeAdapter });
56+
}
57+
58+
public static setAdapter(adapter: KeyvStoreAdapter): void {
59+
CacheHttpInstance.storeAdapter = adapter;
3860
}
3961

4062
public static getCacheHttp(): Keyv {
@@ -44,6 +66,11 @@ class CacheHttpInstance {
4466
}
4567
}
4668

69+
const _httpFileAdapter = new KeyvFile({ filename: cacheHttpFilePath });
70+
71+
const httpLruAdapter = new KeyvLruManagedTtl({ max: cacheHttpLruItems });
72+
CacheHttpInstance.setAdapter(httpLruAdapter);
73+
4774
export const getCacheHttp = CacheHttpInstance.getCacheHttp;
4875

4976
export const cacheDatabaseWrapper = async <T, A extends any[]>(

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"jsdom": "^25.0.1",
4747
"keyv": "^5.1.2",
4848
"keyv-file": "^5.0.3",
49+
"keyv-lru": "^3.0.4",
4950
"lucide-react": "^0.456.0",
5051
"next": "^15.0.0",
5152
"next-plausible": "^3.12.4",

yarn.lock

+12
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,13 @@ keyv-file@^5.0.3:
30423042
fs-extra "^4.0.1"
30433043
tslib "^1.14.1"
30443044

3045+
keyv-lru@^3.0.4:
3046+
version "3.0.4"
3047+
resolved "https://registry.yarnpkg.com/keyv-lru/-/keyv-lru-3.0.4.tgz#95ec97077a18d9a6ceb16a48b6530c26c5ce9099"
3048+
integrity sha512-4XALKaBM/3nZr0o0Hskq+SPS2tBkfZXYQRDl0SqywIimZf0wrrMEgSfoOM7eNuKXnICIOHs3exPucGKDYAp/8g==
3049+
dependencies:
3050+
tiny-lru "^1.6.1"
3051+
30453052
keyv@^4.5.3:
30463053
version "4.5.4"
30473054
resolved "https://registry.yarnpkg.com/keyv/-/keyv-4.5.4.tgz#a879a99e29452f942439f2a405e3af8b31d4de93"
@@ -4279,6 +4286,11 @@ tiny-invariant@^1.3.1:
42794286
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.3.3.tgz#46680b7a873a0d5d10005995eb90a70d74d60127"
42804287
integrity sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==
42814288

4289+
tiny-lru@^1.6.1:
4290+
version "1.6.4"
4291+
resolved "https://registry.yarnpkg.com/tiny-lru/-/tiny-lru-1.6.4.tgz#ee5226a29a7b734ef8aee47673dd5079bf987441"
4292+
integrity sha512-Et+J3Css66XPSLWjLF9wmgbECsGiExlEL+jxsFerTQF6N6dpxswDTPAfIrAbQKO5c1uhgq2xvo5zMk1W+kBDNA==
4293+
42824294
tldts-core@^6.1.58:
42834295
version "6.1.58"
42844296
resolved "https://registry.yarnpkg.com/tldts-core/-/tldts-core-6.1.58.tgz#f0b5c1fcb2e214f558c7cb380fb1e6f4b2459d8b"

0 commit comments

Comments
 (0)