Open Source LRU Cache for Deno.
version 1.0.0
Status · Description · TODO List · Usage · Contributing
Cobalt is currently in 1.0.0.
Cobalt is an Open Source LRU Cache for Deno.
- Basic LRUEntry
- Basic LRUCache (get, set methods)
- forEach, forEachReverse methods
- has, pop, reset methods
- toArray, toArrayReverse methods
- keys, values methods
- entries staleness management
- peek, load methods
- support different key/value types (or at least support serialization natively)
In order to import Cobalt in your Deno code you must import it at the top:
import { Cobalt } from "https://deno.land/x/cobalt/mod.ts";
After you've imported Cobalt, initialize it with the following code:
const cobalt = new Cobalt(); // With default 1000 capacity
const cobaltExpanded = new Cobalt({capacity: 10000}); // Override the default capacity
const cobaltStale = new Cobalt({capacity: 10000, allowStale: true, maxAge: 10}); // Creates a new cobalt cache that allows staleness with objects lasting max 10 seconds
With an instantiated Cobalt object you can set, get and remove a key/value pair:
cobalt.set('key', 'test'); // Sets the value 'test' for the key 'test'
cobalt.get('key'); // Returns 'test'
cobalt.remove('key'); // Removes the key from the cache
cobalt.get('key'); // Returns null
These are some of the functions that you can use with a Cobalt object:
cobalt.size; // returns the cache size
cobalt.reset(); // resets th
6BB4
e cache to its starting configuration
cobalt.keys(); // returns an array with all the keys in the cache
cobalt.values(); // returns an array with all the values in the cache
cobalt.has('key'); // returns true if the cache has the 'key' key, false otherwise
cobalt.pop(); // removes the last entry (tail) and returns its value
cobalt.forEach((entry, index) => /* do something */); // iterates through the cache from the MRU to the LRU
cobalt.forEachReverse((entry, index) => /* do something */); // iterates through the cache from the LRU to the MRU
cobalt.toArray(); // returns the cache as an array of entries
cobalt.toArrayReverse(); // returns the cache as a reversed array of entries
In order to run the tests, just clone the directory:
git clone https://github.com/CIDARO/cobalt.git
And then run the following code:
cd cobalt
deno test mod.test.ts
Hopefully everything will be ok!
We welcome community contributions!
Please check out our open issues to get started.
If you discover something that could potentially impact security, please notify us immediately by sending an e-mail at support@cidaro.com. We'll get in touch with you as fast as we can!