
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
custom-cache-decorator
Advanced tools
A decorator to wrap methods and functions with caching capabilities.
A TypeScript library providing a customizable cache decorator for methods. This library allows you to easily cache method results with configurable caching mechanisms.
Install the library via npm:
npm install custom-cache-decorator
or yarn
yarn add custom-cache-decorator
Here's a basic example of how to use the cache decorator in your TypeScript project.
import { createCacheDecorator } from 'custom-cache-decorator';
Create a cache decorator with custom getItem
and setItem
functions:
import { createCacheDecorator } from 'custom-cache-decorator';
const inMemoryCache = new Map<string, any>();
const cacheDecorator = createCacheDecorator({
getItem: async (key) => inMemoryCache.get(key),
setItem: async (key, value) => inMemoryCache.set(key, value),
});
Use the created cache decorator on your methods:
class ExpensiveOperations {
@cacheDecorator({ ttl: 3000 })
async heavyComputation(arg1: number, arg2: number): Promise<number> {
console.log('Performing heavy computation...');
// Simulate heavy computation
return new Promise((resolve) =>
setTimeout(() => resolve(arg1 * arg2), 1000)
);
}
}
(async () => {
const operations = new ExpensiveOperations();
console.log(await operations.heavyComputation(2, 3)); // Performs computation
console.log(await operations.heavyComputation(2, 3)); // Uses cached result
})();
also make sure you have experimentalDecorators
set to true
in your tsconfig.json
file
{
"compilerOptions": {
...
"experimentalDecorators": true
...
}
}
Here's an example of using the cache decorator with Redis as the caching backend.
import { createCacheDecorator } from 'custom-cache-decorator';
import { createClient } from 'redis';
(async () => {
const client = createClient();
await client.connect();
const redisCacheDecorator = createCacheDecorator({
getItem: async (key) => {
const value = await client.get(key);
return value ? JSON.parse(value) : undefined;
},
setItem: async (key, value, { ttl }: { ttl: number }) => {
await client.setEx(key, ttl, JSON.stringify(value));
},
});
class ExpensiveOperations {
@redisCacheDecorator({ ttl: 60 })
async heavyComputation(arg1: number, arg2: number): Promise<number> {
console.log('Performing heavy computation...');
// Simulate heavy computation
return new Promise((resolve) =>
setTimeout(() => resolve(arg1 * arg2), 1000)
);
}
}
const operations = new ExpensiveOperations();
console.log(await operations.heavyComputation(2, 3)); // Performs computation
console.log(await operations.heavyComputation(2, 3)); // Uses cached result
await client.disconnect();
})();
createCacheDecorator(options: CreateCacheDecoratorOptions<T, O>): (decoratorArgs: O) => MethodDecorator
Creates a cache decorator.
options
: Configuration options for the cache decorator.
getItem
: Function to retrieve an item from the cache.setItem
: Function to store an item in the cache.generateKey
(optional): Custom function to generate cache keys. Defaults to defaultGenerateKey
.A cache decorator function to be applied to class methods.
defaultGenerateKey(className: string, methodName: string, args: unknown[]): string
Default function to generate cache keys.
className
: The name of the class.methodName
: The name of the method.args
: The arguments passed to the method.A string representing the cache key.
You can customize the cache key generation with the generateKey
arg.
import {
createCacheDecorator,
defaultGenerateKey,
} from 'custom-cache-decorator';
const customKeyDecorator = createCacheDecorator({
getItem: async (key) => inMemoryCache.get(key),
setItem: async (key, value) => inMemoryCache.set(key, value),
generateKey: (className, methodName, args) =>
`${className}_${methodName}_${args.join('_')}`,
});
class ExpensiveOperations {
@customKeyDecorator({})
async heavyComputation(arg1: number, arg2: number): Promise<number> {
console.log('Performing heavy computation...');
return new Promise((resolve) =>
setTimeout(() => resolve(arg1 * arg2), 1000)
);
}
}
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License.
:star:
FAQs
A decorator to wrap methods and functions with caching capabilities.
We found that custom-cache-decorator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.