3.0.0 • Published 1 year ago

@serum-enterprises/cache v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Cache

This Node.js module, Cache, is a simple yet efficient in-memory Cache implementation using Node's Buffers for Data. This Library offers a Base Class called MLC (Manual LifeCycle) that does no Cache Invalidation by itself, as well as the 2 very popular Cache policies LFU (Least Frequently Used) and LRU (Least Recently Used).

Features

  • MLC Strategy: Manual Lifecycle, no Cache Invalidation by itself. If an Item is too big for the Cache, set will silently fail.
  • LFU Strategy: Least Frequently Used Items get removed first when the Cache reaches its size limit.
  • LRU Strategy: Least Recently Used Items get removed first when the Cache reaches its size limit.
  • Maximum Cache Size given in Bytes. A value of 0 effectively disables the Cache.
  • Stores NodeJS Buffers as Values.
  • Methods to get, set, rename, delete and check existence (has) of Items in the Cache.
  • A convenient Method to clear the Cache.

Installation

You can install this Library in your Project using npm:

npm install --save @serum-enterprises/cache

Usage

Import the relevant Classes into your file:

const { MLC, LFU, LRU } = require('@serum-enterprises/cache');

Note: All Classes have the same Interface and can be used interchangeably. For the rest of this Documentation, we will use Cache to refer to any Implementation.

Note: Both LFU and LRU are derived from MLC, which only handles Cache Size, while LFU and LRU handle Cache Invalidation as well.

Create a new Cache

// Create a new Cache with a maximum size of 1MB (1024 * 1024 Bytes)
const cache = new Cache(1024 * 1024);

You can specify the maximum Cache size in Bytes while creating the Cache object. A Value of 0 effectively disables the Cache.

Get and Set values

let bufferValue = Buffer.from('Hello, World!');
cache.set('greeting', bufferValue);

let retrievedBuffer = cache.get('greeting');
console.log(retrievedBuffer.toString()); // Prints: 'Hello, World!'

The set Method is used to store a Buffer in the Cache, while the get Method retrieves the stored Buffer. On the LFU and LRU Strategies, the set Method also ensures that the new Value fits into the Cache by removing the least frequently or least recently used Items respectively if necessary. On the MLC Strategy, if there is not enough Space, the set Method will silently fail. If the Buffer Value is larger than the maximum Cache size, it won't be added to the Cache and the set Method will silently fail. The get Method returns undefined if the Key is not found in the Cache. If the Key already exists in the Cache, the set Method will overwrite the existing Value.

Other operations

// Check if an Item exists based on its Key
cache.has('greeting');
// Rename an Item (change its Key)
cache.rename('greeting', 'hello'); 
// Get all Keys as an Array
cache.keys();
// Delete an Item based on its Key
cache.delete('hello');
// Clear the Cache
cache.clear();

The Cache provides Methods for checking if an Item exists (has), renaming an Item (rename), getting all Keys (keys), deleting an Item (delete), and clearing the entire Cache (clear).

Error Handling

Methods will throw a TypeError if an Argument has a wrong Data Type.

Tests

Tests for this Library are written using Jest and have 100% Code Coverage.

To run the Tests, use the following Command:

npm run test

License

MIT License

Copyright (c) 2023 Serum Enterprises

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3.0.0

1 year ago

2.0.0

1 year ago

1.0.0

2 years ago