Discover CacheXS, a remarkably efficient library for JavaScript and Bun, ideal for developers prioritizing performance and simplicity. Written in TypeScript, and it uses bun redis under the hood, it offers seamless integration for both TypeScript and JavaScript projects. Its compact size belies its powerful functionality, making it perfect for lightweight, modern applications. With ESM compatibility, CacheXS aligns with contemporary development practices, ensuring its utility in a range of projects from small-scale to complex.
- Installation
- Usage
- Documentation
- Support and Questions
- Contribution
- Guidelines for Contributions
- License
Getting up and running with CacheXS is a breeze. Choose your preferred package manager from the options below and follow the simple installation steps:
bun i cachexs
Once you have installed CacheXS, integrating it into your project is straightforward. Below are the basic steps to get you started:
First, import CacheXS into your JavaScript or TypeScript file:
import CacheXS from 'cachexs'Then create a new instance for CacheXS and pass the configuration to it:
const cacheXS = new CacheXS({
redisOptions: {
// Connection timeout in milliseconds (default: 10000)
connectionTimeout: 5000,
// Idle timeout in milliseconds (default: 0 = no timeout)
idleTimeout: 30000,
// Whether to automatically reconnect on disconnection (default: true)
autoReconnect: true,
// Maximum number of reconnection attempts (default: 10)
maxRetries: 10,
// Whether to queue commands when disconnected (default: true)
enableOfflineQueue: true,
// Whether to automatically pipeline commands (default: true)
enableAutoPipelining: true,
// TLS options (default: false)
tls: true,
// Alternatively, provide custom TLS config:
// tls: {
// rejectUnauthorized: true,
// ca: "path/to/ca.pem",
// cert: "path/to/cert.pem",
// key: "path/to/key.pem",
// }
},
})And then you can use it like this:
const cachedData = cacheXS.get('foo') // -> Bar
The CacheXS package comes with a comprehensive set of features designed to make cache in your application straightforward and efficient. This section provides an overview of its capabilities and guides on how to use them effectively.
-
- Simple Cache Management: Easily cache the data in Redis with simple functions.
- Expiration Control: Set, update, and check expiration times for cached entries.
- Pattern Matching: Find, retrieve, and delete cache entries using powerful pattern matching with
scanandkeysmethods. - Conditional Caching: Use
setIfNotExists,getOrSet, andgetOrSetForeverfor intelligent cache operations. - Bulk Operations: Efficiently work with multiple cache entries at once with pattern-based operations.
-
-
Refer to the Installation section for instructions on how to install CacheXS using various package managers.
-
You can create a new instance of CacheXS and pass the configuration for it directly like this example below:
const cacheXS = new CacheXS({ redisClient: redis, namespace: 'myCache', enableDebug: true, })
Alternatively, you can split the creation of the new instance and the configuration, useful when split up into different modules for bootstrapping.
const cacheXS = new CacheXS() cacheXS.configure({ redisClient: redis, namespace: 'myCache', enableDebug: true, })
-
-
-
getRetrieves the value associated with the specified key from the cache.const stringValue = await cacheXS.get<string>('myKey') // -> myValue const stringValue = await cacheXS.get<number>('myKey') // -> 123 const objectValue = await cacheXS.get<MyObject>('myKey') // -> { name: 'John', age: 30 }
-
set: Sets a value in the cache.await cacheXS.set('myKey', 'myValue') await cacheXS.set('myKey', 123) await cacheXS.set('myKey', { name: 'John', age: 30 }, 360) // expires in 360 seconds
-
setForeverSets a value in the cache foreverawait cacheXS.setForever('user', { name: 'John Doe', age: 30 })
-
setIfNotExistsSets a value in the cache only if the key does not already exist.await cacheXS.setIfNotExists('myKey', 'myValue', 360) // expires in 360 seconds
-
getOrSetRetrieves the value associated with the specified key from the cache. If the value does not exist, it sets the value to the provided fallback value and returns it.const username = await cacheXS.getOrSet('myKey', 'myValue', 360) // expires in 360 seconds
-
getOrSetForeverRetrieves the value associated with the specified key from the cache. If the value does not exist, it sets the specified fallback value in the cache and returns it.value = await cacheXS.getOrSetForever('myKey', 'defaultValue')
-
incrementIncrements the value of a key by one. If the key does not exist, it will be set to 0 before performing the operation. Returns the new value after incrementing.const newValue = await cacheXS.increment('count') // -> 1 (if key doesn't exist) const nextValue = await cacheXS.increment('count') // -> 2
-
decrementDecrements the value of a key by one. If the key does not exist, it will be set to 0 before performing the operation. Returns the new value after decrementing.const newValue = await cacheXS.decrement('count') // -> -1 (if key doesn't exist) const nextValue = await cacheXS.decrement('count') // -> -2
-
expireSets the expiration time for a key.await cacheXS.expire('myKey', 60) // expires in 60 seconds
-
expireNowExpires a key immediately.await cacheXS.expireNow('myKey')
-
ttlGets the time to live for a key in seconds.const ttl = await cacheXS.ttl('myKey') // -> 60
-
deleteDeletes a cache entry by its key.await cacheXS.delete('myKey')
-
deleteManyDeletes multiple cache entries specified by the given keys.await cacheXS.deleteMany(['myKey', 'myKey2', 'myKey3'])
-
clearClears all cache entries in the CacheXS instance.await cacheXS.clear()
-
existsChecks if a key exists in the cache.await cacheXS.exists('myKey') // -> true || false
-
missingChecks if a key is missing in the cache.await cacheXS.missing('myKey') // -> true || false
-
scanFinds keys matching a pattern using the SCAN command (non-blocking, recommended for production).const userKeys = await cacheXS.scan('user:*') // -> ['user:123', 'user:456'] const sessionKeys = await cacheXS.scan('session:*', 50) // with custom count
-
keysFinds keys matching a pattern using the KEYS command (blocking, use with caution in production).const userKeys = await cacheXS.keys('user:*') // -> ['user:123', 'user:456']
-
getByPatternRetrieves multiple values by pattern matching.const userData = await cacheXS.getByPattern<User>('user:*') // -> { 'user:123': {...}, 'user:456': {...} } // Use KEYS command instead of SCAN const configData = await cacheXS.getByPattern('config:*', false)
-
deleteByPatternDeletes keys matching a pattern.const deletedCount = await cacheXS.deleteByPattern('session:*:expired') console.log(`Deleted ${deletedCount} expired sessions`) // Use KEYS command instead of SCAN const count = await cacheXS.deleteByPattern('temp:*', false)
-
concatenateKeyConcatenates the given key with the namespace and returns the resulting string.await cacheXS.concatenateKey('myKey') // -> 'CacheXS:myKey'
-
redisClientGets the Redis connection.cacheXS.redisClient // -> Bun redis instance
-
redisUrlGets the Redis URL.cacheXS.redisUrl // -> 'redis://user:pass@localhost:6379'
-
redisOptionsGets the Redis configuration.cacheXS.redisOptions // -> { host: 'localhost', port: 6379, password: '' }
-
expiresInGets the expiration time in seconds.cacheXS.expiresIn // -> 60
-
namespaceGets the namespace of the cache.cacheXS.namespace // -> 'CacheXS
-
isDebugEnabledGets a value indicating whether debug mode is enabled.cacheXS.isDebugEnabled // -> True || False
-
If you have any questions or need support while using CacheXS, feel free to open an issue on our GitHub repository or reach out to the community for help.
For the complete and detailed guide, please refer to our official documentation.
First off, thank you for considering contributing to CacheXS! It's people like you who make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
There are many ways you can contribute to CacheXS, even if you're not a technical person:
- Submit Bug Reports: If you find a bug, please open an issue. Remember to include a clear description and as much relevant information as possible.
- Feature Suggestions: Have an idea for a new feature or an improvement? Open an issue and tag it as a feature request.
- Code Contributions: Interested in adding a feature or fixing a bug? Awesome! Please open a pull request with your changes.
- Documentation: Good documentation is key to any project. If you see something unclear or missing, feel free to submit a pull request.
- Spread the Word: Share CacheXS with your network and let others know about it.
Ensure you use a consistent coding style with the rest of the project. Write clear, readable, and concise code. Add unit tests for new features to ensure reliability and maintainability. Update the README or documentation with details of changes, this includes new environment variables, exposed ports, useful file locations, and container parameters. Increase the version numbers in any example files and the README to the new version that this Pull Request would represent.
CacheXS is licensed under the MIT License. This license permits use, modification, and distribution, free of charge, for both private and commercial purposes. It also offers a good balance between protecting the author's rights and allowing for flexibility and freedom in the use of the software.