import { CacheFactory } from "@daiso-tech/core/cache";
import type { IDatabaseCacheAdapter } from "@daiso-tech/core/cache/contracts";
import { MemoryCacheAdapter, RedisCacheAdapter, SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
import { Serde } from "@daiso-tech/core/serde";
import type { ISerde } from "@daiso-tech/core/serde/contracts";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters";
import { KeyPrefixer, type ISqliteDatabase, type AsyncFactoryFn } from "@daiso-tech/core/utilities";
import Redis from "ioredis"
import Sqlite from "better-sqlite3";
function cahceAdapterFactory(database: ISqliteDatabase, serde: ISerde<string>): AsyncFactoryFn<string, IDatabaseCacheAdapter> {
return async (prefix) => {
const cacheAdapter = new SqliteCacheAdapter({
database,
serde,
tableName: `cache_${prefix}`
});
await cacheAdapter.init();
return cacheAdapter;
}
}
const database = new Sqlite("local.db");
const serde = new Serde(new SuperJsonSerdeAdapter());
const cacheFactory = new CacheFactory({
keyPrefixer: new KeyPrefixer("cache"),
adapters: {
sqlite: cahceAdapterFactory(database, serde),
memory: new MemoryCacheAdapter(),
redis: new RedisCacheAdapter({
database: new Redis("YOUR_REDIS_CONNECTION"),
serde,
}),
},
defaultAdapter: "memory",
import { CacheFactory } from "@daiso-tech/core/cache";
import type { IDatabaseCacheAdapter } from "@daiso-tech/core/cache/contracts";
import { MemoryCacheAdapter, RedisCacheAdapter, SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
import { Serde } from "@daiso-tech/core/serde";
import type { ISerde } from "@daiso-tech/core/serde/contracts";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters";
import { KeyPrefixer, TimeSpan, type ISqliteDatabase, type AsyncFactoryFn } from "@daiso-tech/core/utilities";
import Redis from "ioredis"
import Sqlite from "better-sqlite3";
function cahceAdapterFactory(database: ISqliteDatabase, serde: ISerde<string>): AsyncFactoryFn<string, IDatabaseCacheAdapter> {
return async (prefix) => {
const cacheAdapter = new SqliteCacheAdapter({
database,
serde,
tableName: `cache_${prefix}`
});
await cacheAdapter.init();
return cacheAdapter;
}
}
const database = new Sqlite("local.db");
const serde = new Serde(new SuperJsonSerdeAdapter());
const cacheFactory = new CacheFactory({
keyPrefixer: new KeyPrefixer("cache"),
adapters: {
sqlite: cahceAdapterFactory(database, serde),
memory: new MemoryCacheAdapter(),
redis: new RedisCacheAdapter({
database: new Redis("YOUR_REDIS_CONNECTION"),
serde,
}),
},
defaultAdapter: "memory",
});
// Will add key to cache using the default adapter which is MemoryCacheAdapter
await cacheFactory
.use()
.add("a", 1);
// Will add key to cache using the redis adapter which is RedisCacheAdapter
await cacheFactory
.use("redis")
.add("a", 1);
// You can change the default settings of the returned Cache instance.
await cacheFactory
.setDefaultTtl(TimeSpan.fromMinutes(2))
.use("sqlite")
.add("a", 1);
The
CacheFactory
class is immutable.IMPORT_PATH:
"@daiso-tech/core/cache"