@cachehub/core
v1.0.2
Published
## Overview The core package of the caching library provides the foundational interfaces and factory pattern for creating cache implementations. It defines the contracts that all cache adapters must adhere to, ensuring a consistent API for interacting wit
Readme
Core Cache Library
Overview
The core package of the caching library provides the foundational interfaces and factory pattern for creating cache implementations. It defines the contracts that all cache adapters must adhere to, ensuring a consistent API for interacting with different caching strategies.
Benefits
- Unified Interface: All cache implementations conform to the same interface, making it easy to switch between them.
- Factory Pattern: The factory pattern allows for dynamic creation of cache instances based on configuration.
Interfaces
CacheInterface
The CacheInterface defines the methods that all cache implementations must implement:
set(key: string, value: TypeValue, expiresAfter?: number): Promise<boolean>get(key: string): Promise<TypeValue | undefined>delete(key: string): Promise<boolean>has(key: string): Promise<boolean>
CacheFactoryInterface
The CacheFactoryInterface defines the method for creating cache instances:
createCache(config: TypeConfig): Promise<CacheInterface>
Usage
To use the core functionalities, follow these steps:
- Import the necessary interfaces and factory from the core package.
- Implement your custom cache adapter by extending the
CacheInterface. - Create a factory class that implements the
CacheFactoryInterface.
Example
import { CacheInterface, CacheFactoryInterface } from '@cachehub/core';
class MyCustomCache implements CacheInterface {
// Implement the required methods: set, get, delete, has
}
class MyCustomCacheFactory implements CacheFactoryInterface {
public async createCache(config: any): Promise<CacheInterface> {
return new MyCustomCache();
}
}To make your package self-registering, ensure your package registers the factory with the CacheFactoryRegistry and exports the factory class as the default export in index.ts. For example:
import { CacheFactoryRegistry } from '@cachehub/core';
import { MyCustomCacheFactory } from './Factory';
CacheFactoryRegistry.registerCacheFactory('my-custom-cache', MyCustomCacheFactory);
// default export so that core factory can use it directly
export default MyCustomCacheFactory;Generic Type Usage
The CacheInterface supports generic types for type-safe storage and retrieval of values. Here are examples of how to use generics with the cache methods:
// Using primitive types
await cache.set<string>('stringKey', 'value');
const stringValue = await cache.get<string>('stringKey');
await cache.set<number>('numberKey', 42);
const numberValue = await cache.get<number>('numberKey');
await cache.set<boolean>('booleanKey', true);
const booleanValue = await cache.get<boolean>('booleanKey');
// Using complex objects
interface User {
id: number;
name: string;
email: string;
}
const user: User = {
id: 1,
name: 'John Doe',
email: '[email protected]'
};
await cache.set<User>('userKey', user);
const userValue = await cache.get<User>('userKey');
// Using arrays
await cache.set<string[]>('arrayKey', ['a', 'b', 'c']);
const arrayValue = await cache.get<string[]>('arrayKey');When implementing a custom cache adapter, make sure to properly handle the generic types in your implementation to maintain type safety throughout the application.
Folder Structure
The folder structure for each package typically includes:
src/: Contains the source code for the package, including:Factory/: Contains factory classes for creating cache handler.Cache/: Contains classes that implement the cache object.Type/: Contains type definitions and interfaces.
tests/: Contains unit tests for the package.README.md: Documentation for the package..env: Environment variables for configuration.
Contributing
Contributions are welcome! Feel free to fork this repository and submit pull requests. Before submitting, please ensure your code passes all linting and unit tests.
You can format code using:
pnpm formatYou can run unit tests using:
pnpm test