Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aomex/cache

Package Overview
Dependencies
Maintainers
0
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aomex/cache - npm Package Compare versions

Comparing version 3.7.2 to 3.8.0

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

# [3.8.0](https://github.com/aomex/aomex/compare/v3.7.2...v3.8.0) (2024-10-26)
### Features
* **cache:** 增加方法装饰器 ([e1ecc7f](https://github.com/aomex/aomex/commit/e1ecc7f3b995e77f174f6f2d1fe3f35933c662f4))
## [3.7.2](https://github.com/aomex/aomex/compare/v3.7.1...v3.7.2) (2024-10-22)

@@ -8,0 +19,0 @@

26

dist/index.d.ts

@@ -18,3 +18,5 @@ declare abstract class CacheAdapter {

declare namespace Caching {
type Types = string | number | object | boolean;
type Types = string | number | unknown[] | readonly unknown[] | {
[K: string]: unknown;
} | boolean | Map<any, any> | Set<any>;
}

@@ -110,2 +112,24 @@ declare class Caching<T extends CacheAdapter = CacheAdapter> {

deleteAll(): Promise<boolean>;
/**
* 方法装饰器,自动获取数据,如果不存在则请求原始数据并自动保存。在请求原始数据期间,任意相同请求都会被截流,并共享第一个请求
* ```typescript
* const cache = new Caching(memoryAdapter());
*
* class MyClass {
* \@cache.decorate({ key: (id) => `key_${id}`, duration: 60_000 })
* async getData(id: number) {
* const result = await queryDB({ id: id });
* return result;
* }
* }
* ```
*/
decorate<T extends Caching.Types | null, P extends (...args: any[]) => Promise<T>, Q extends NonNullable<T>>(opts: {
key: string | ((...args: Parameters<P>) => string);
duration?: number;
/**
* 当数据源返回null或者undefined时,则返回默认值
*/
defaultValue?: Q;
}): (originalMethod: P, _context: ClassMethodDecoratorContext) => (this: object, ...args: Parameters<P>) => Promise<any>;
protected encodeValue(value: Caching.Types): string;

@@ -112,0 +136,0 @@ protected decodeValue(value: string | null, defaultValue?: Caching.Types): any;

@@ -133,2 +133,40 @@ // src/cache-adapter.ts

}
/**
* 方法装饰器,自动获取数据,如果不存在则请求原始数据并自动保存。在请求原始数据期间,任意相同请求都会被截流,并共享第一个请求
* ```typescript
* const cache = new Caching(memoryAdapter());
*
* class MyClass {
* \@cache.decorate({ key: (id) => `key_${id}`, duration: 60_000 })
* async getData(id: number) {
* const result = await queryDB({ id: id });
* return result;
* }
* }
* ```
*/
decorate(opts) {
const instance = this;
const fetching = {};
const { key: getKey, duration, defaultValue = null } = opts;
return (originalMethod, _context) => {
return async function(...args) {
const key = typeof getKey === "string" ? getKey : getKey.apply(this, args);
let value = await instance.get(key);
if (value === null) {
if (fetching[key]) {
value = await fetching[key];
} else {
fetching[key] = originalMethod.apply(this, args);
value = await fetching[key];
if (value !== null) {
await instance.set(key, value, duration);
}
Reflect.deleteProperty(fetching, key);
}
}
return value === null ? defaultValue : value;
};
};
}
encodeValue(value) {

@@ -135,0 +173,0 @@ if (value instanceof Map) {

6

package.json
{
"name": "@aomex/cache",
"version": "3.7.2",
"version": "3.8.0",
"description": "缓存基础类",

@@ -34,6 +34,6 @@ "type": "module",

"devDependencies": {
"@aomex/core": "^3.7.2",
"@aomex/internal-tools": "^3.7.2"
"@aomex/core": "^3.8.0",
"@aomex/internal-tools": "^3.8.0"
},
"scripts": {}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc