-
Notifications
You must be signed in to change notification settings - Fork 1
API Reference
Welcome to the SnapCache API Reference! This section provides detailed information about the methods available in SnapCache, including their parameters and return values.
Creates a new instance of SnapCache.
-
options: An object to configure the cache.
-
maxSize (Number): The maximum number of items allowed in the cache. (Default:
100
) -
defaultTTL (Number): The default time-to-live for items in milliseconds. (Default:
60000
)
-
maxSize (Number): The maximum number of items allowed in the cache. (Default:
const cache = new SnapCache({ maxSize: 200, defaultTTL: 30000 });
Stores a value in the cache with the specified key.
- key (String): The unique key under which the value is stored.
- value (Object): The value to cache (can be any object).
-
options (Object, optional): Configuration for this cache entry.
- ttl (Number): The time-to-live for this entry in milliseconds. Overrides the default TTL.
undefined
cache.set('user:456', { name: 'Bob' }, { ttl: 120000 }); // 2 minutes TTL
Retrieves a value from the cache by its key.
- key (String): The key of the cached value.
-
Object: The cached value if it exists and hasn't expired; otherwise, returns
null
.
const user = cache.get('user:456');
console.log(user); // Output: { name: 'Bob' } or null (if expired)
Deletes a specific item from the cache.
- key (String): The key of the cached value to delete.
-
Boolean:
true
if the item was successfully deleted; otherwise, returnsfalse
if the item didn't exist.
const deleted = cache.delete('user:456');
console.log(deleted); // Output: true or false
Clears all items from the cache.
undefined
cache.clear(); // Clears the entire cache
Returns the current number of items in the cache.
- Number: The current size of the cache.
console.log(cache.size()); // Output: current number of items
The SnapCache API provides a straightforward way to manage in-memory caching in your Node.js applications. With easy-to-use methods for setting, getting, deleting, and clearing cache entries, SnapCache helps you enhance the performance and efficiency of your applications.
For more information, check out the Usage Guide for practical examples and use cases!
📄 License: MIT License
📞 Support: If you have any questions or need assistance, feel free to reach out on our Discord.