Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/cache #85

Merged
merged 23 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2743196
Improved ICacheAdapter contract
yousif-khalil-abdulkarim Jan 21, 2025
ca98cd1
Improved ICacheFactory contract documentation example
yousif-khalil-abdulkarim Jan 21, 2025
2e2f82f
Improved ICache contract documentation
yousif-khalil-abdulkarim Jan 21, 2025
e474ea6
Removed unused types
yousif-khalil-abdulkarim Jan 21, 2025
bdc55d1
Improved/updated the cacheAdapterTestSuite
yousif-khalil-abdulkarim Jan 21, 2025
c81887c
Updated the cacheTestSuite
yousif-khalil-abdulkarim Jan 21, 2025
b8b37c9
Updated KyselySqliteCacheAdapter
yousif-khalil-abdulkarim Jan 21, 2025
06534ca
Updated LibsqlCacheAdapter
yousif-khalil-abdulkarim Jan 21, 2025
72bd0a9
Updated LibsqlCacheAdapter tests
yousif-khalil-abdulkarim Jan 21, 2025
a3708b0
Updated MemoryCacheAdapter
yousif-khalil-abdulkarim Jan 21, 2025
f29020b
Updated MemoryCacheAdapter tests
yousif-khalil-abdulkarim Jan 21, 2025
d7d1d67
Updated MongodbCacheAdapter
yousif-khalil-abdulkarim Jan 21, 2025
ea2195f
Updated MongodbCacheAdapter tests
yousif-khalil-abdulkarim Jan 21, 2025
c06a166
Updated NoOpCacheAdapter
yousif-khalil-abdulkarim Jan 21, 2025
35c98ab
Updated RedisCacheAdapter
yousif-khalil-abdulkarim Jan 21, 2025
9cc84fa
Updated RedisCacheAdapter tests
yousif-khalil-abdulkarim Jan 21, 2025
af27db5
Updated SqliteCacheAdapter
yousif-khalil-abdulkarim Jan 21, 2025
7046ed4
Updated SqliteCacheAdapter tests
yousif-khalil-abdulkarim Jan 21, 2025
02f9ab4
Updated Cache class
yousif-khalil-abdulkarim Jan 21, 2025
882be6a
Updated Cache class tests
yousif-khalil-abdulkarim Jan 21, 2025
794bfe1
Updated CacheFactory class tests
yousif-khalil-abdulkarim Jan 21, 2025
2b54773
Update packge.json and removed unused packages
yousif-khalil-abdulkarim Jan 21, 2025
014ae66
Added changeset file
yousif-khalil-abdulkarim Jan 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/brown-mangos-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@daiso-tech/core": minor
---

## Changes
- Moved cache group logic from the <i>Cache</i> class into the adapters classes.
- **Key Impact**: Each adapter is now required to implement the <i>getGroup</i> and <i>withGroup</i> methods.
- This change enhances flexibility for adapter-specific logic.
210 changes: 95 additions & 115 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@
"typedoc-plugin-merge-modules": "^6.0.0",
"typescript": "^5.5.4",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^2.0.5",
"zod": "^3.24.1"
"vitest": "^3.0.2"
},
"peerDependencies": {
"@libsql/client": "^0.4.1",
Expand All @@ -95,16 +94,11 @@
},
"@libsql/client": {
"optional": true
},
"zod": {
"optional": true
}
},
"dependencies": {
"@libsql/kysely-libsql": "^0.4.0",
"escape-string-regexp": "^5.0.0",
"kysely": "^0.27.0",
"superjson": "^2.2.0",
"zod": "^3.24.0"
"superjson": "^2.2.0"
}
}
29 changes: 23 additions & 6 deletions src/cache/contracts/cache-adapter.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module Cache
*/

import type { TimeSpan } from "@/utilities/_module";
import type { OneOrMore, TimeSpan } from "@/utilities/_module";
import {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type TypeCacheError,
Expand All @@ -15,13 +15,18 @@ import {
*/
export type ICacheAdapter<TType = unknown> = {
/**
* The <i>get</i> returns the value of <i>keys</i> that are found otherwise null will be returned.
* The <i>exists</i> returns true if the <i>key</i> if exists otherwise false will be returned.
*/
exists(key: string): PromiseLike<boolean>;

/**
* The <i>get</i> returns the <i>key</i> value if found otherwise null will be returned.
*/
get(key: string): PromiseLike<TType | null>;

/**
* The <i>add</i> method add a new <i>key</i>. Returns true if the <i>key</i> where added otherwise false will be returned.
* You can provide a <i>ttl</i> value. If null is passed, the item will not expire.
* You must provide a <i>ttl</i> value. If null is passed, the item will not expire.
*/
add(key: string, value: TType, ttl: TimeSpan | null): PromiseLike<boolean>;

Expand All @@ -31,8 +36,9 @@ export type ICacheAdapter<TType = unknown> = {
update(key: string, value: TType): PromiseLike<boolean>;

/**
* The <i>put</i> method replaces a <i>key</i> that exists or adds <i>key</i> that do not exists. Returns true if the <i>key</i> where updated otherwise false is returned.
* You can provide a <i>ttl</i> value. If null is passed, the item will not expire.
* The <i>put</i> method replaces a <i>key</i> if the <i>key</i> exists including the ttl value or adds <i>key</i> that do not exists with a given <i>ttl</i>.
* Returns true if the <i>key</i> where replaced otherwise false is returned.
* You must provide a <i>ttl</i> value. If null is passed, the item will not expire.
*/
put(key: string, value: TType, ttl: TimeSpan | null): PromiseLike<boolean>;

Expand All @@ -52,5 +58,16 @@ export type ICacheAdapter<TType = unknown> = {
/**
* The <i>clear</i> method removes all keys that starts <i>prefix</i>.
*/
clear(prefix: string): PromiseLike<void>;
clear(): PromiseLike<void>;

/**
* The <i>getGroup</i> method returns the group name.
*/
getGroup(): string;

/**
* The <i>withGroup</i> method returns a new <i>{@link ICacheAdapter}</i> instance that groups keys together.
* Only keys in the group can be updated, removed, or retrieved, leaving keys outside the group unaffected.
*/
withGroup(group: OneOrMore<string>): ICacheAdapter<TType>;
};
2 changes: 1 addition & 1 deletion src/cache/contracts/cache-factory.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type ICacheFactory<TDrivers extends string = string, TType = unknown> = {
* The <i>withTypes</i> method is used to set the value types of the cache.
* @example
* ```ts
* import { type ICacheFactory zodValidator } from "@daiso-tech/core";
* import { type ICacheFactory } from "@daiso-tech/core";
*
* async function main(cacheFactory: ICacheFactory): Promise<void> {
* await cacheFactory
Expand Down
17 changes: 10 additions & 7 deletions src/cache/contracts/cache.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ export type ICache<TType = unknown> = ICacheListenable & {
): LazyPromise<Record<TKeys, boolean>>;

/**
* The <i>put</i> method replaces the key with given <i>value</i> if found. If the <i>key</i> is not found it will just be added. True is returned if the key is found otherwise false will be returned.
* You can provide a <i>ttl</i> value. If null is passed, the item will not expire.
* The <i>put</i> method replaces a <i>key</i> if the <i>key</i> exists including the ttl value or adds <i>key</i> that do not exists with a given <i>ttl</i>.
* Returns true if the <i>key</i> where replaced otherwise false is returned.
* You must provide a <i>ttl</i> value. If null is passed, the item will not expire.
*/
put(key: string, value: TType, ttl?: TimeSpan | null): LazyPromise<boolean>;

/**
* The <i>putMany</i> method replaces the keys that exists. Adds keys that do not exists. Returns true for all the keys that where updated otherwise false is returned.
* The <i>putMany</i> method replaces the keys that exists including their ttl values or adds keys that do not exists.
* Returns true for all the keys that where replaced otherwise false is returned.
*/
putMany<TKeys extends string>(
values: Record<TKeys, WithTtlValue<TType>>,
Expand Down Expand Up @@ -187,18 +189,18 @@ export type ICache<TType = unknown> = ICacheListenable & {
clear(): LazyPromise<void>;

/**
* The <i>getGroup</i> method returns the complete group.
* The <i>getGroup</i> method returns the group name.
* @example
* ```ts
* import type { ICache } from "@daiso-tech/core";
*
* async function main(cache: ICache) {
* // Will be "@root"
* // Will be "@global"
* console.log(cache.getGroup())
*
* const cacheA = cache.withGroup("a");
*
* // Will be "@root/a"
* // Will be "@global/a"
* console.log(cacheA.getGroup())
* }
* ```
Expand All @@ -213,7 +215,8 @@ export type ICache<TType = unknown> = ICacheListenable & {
*/
export type IGroupableCache<TType = unknown> = ICache<TType> & {
/**
* The <i>withGroup</i> method returns new instance of <i>{@link ICache}</i> where all the keys will be prefixed with a given <i>group</i>.
* The <i>withGroup</i> method returns a new <i>{@link ICache}</i> instance that groups keys together.
* Only keys in the group can be updated, removed, or retrieved, leaving keys outside the group unaffected.
* This useful for multitennat applications.
* @example
* ```ts
Expand Down
40 changes: 17 additions & 23 deletions src/cache/contracts/cache.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,43 @@
*/

import type { TimeSpan } from "@/utilities/_module";
import type { ICacheAdapter } from "@/cache/contracts/cache-adapter.contract";

/**
* @group Events
*/
export type CacheEvent = {
group: string;
adapter: ICacheAdapter<any>;
};

/**
* This event will be triggered when cache is cleared.
* @group Events
*/
export type KeysClearedCacheEvent = CacheEvent & {};

export type CacheEvents<TType = unknown> = {
key_found: CacheEvent & {
key_found: {
group: string;
key: string;
value: TType;
};
key_not_found: CacheEvent & {
key_not_found: {
group: string;
key: string;
};
key_added: CacheEvent & {
key_added: {
group: string;
key: string;
value: TType;
ttl: TimeSpan | null;
};
key_updated: CacheEvent & {
key_updated: {
group: string;
key: string;
value: TType;
};
key_removed: CacheEvent & {
key_removed: {
group: string;
key: string;
};
key_incremented: CacheEvent & {
key_incremented: {
group: string;
key: string;
value: number;
};
key_decremented: CacheEvent & {
key_decremented: {
group: string;
key: string;
value: number;
};
keys_cleared: CacheEvent;
keys_cleared: {
group: string;
};
};
Loading
Loading