npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cache-lru

v1.2.0

Published

In-Memory Cache with O(1) Operations and LRU Purging Strategy

Downloads

13,152

Readme

Cache-LRU

In-Memory Cache with O(1) Operations and LRU Purging Strategy

About

Cache-LRU is a very small JavaScript library, providing an in-memory key-indexed object cache data structure with Least-Recently-Used (LRU) object purging strategy and main operations which all have strict O(1), i.e., constant and hence independent of the number of cached objects, time complexity (including the internal LRU purging functionality).

Installation

$ npm install cache-lru

Usage

Main Operations

  • new CacheLRU(): CacheLRU: [O(1)] Create a new cache instance -- with initially no cached objects and an infinite caching limit.

  • CacheLRU#limit(max?: Number): Number: [O(1)] Set a new or get current maximum number of objects in the cache. On setting a new maximum, if previously already more than max objects are in the cache, the length() - max LRU ojects are automatically deleted.

  • CacheLRU#length(): Number: [O(1)] Get number of objects in the cache.

  • CacheLRU#dispose(callback: (key, val, op) => Void): CacheLRU: [O(1)] Set a callback which is called once for every object just before it is disposed off the cache, either because it is replaced by a set() call or deleted by a del() call. The parameters to the callback are the key under which the object was stored, the value of the object which was stored and the operation (either set or del) which caused the object disposal. This is usually used for explicit resource deallocation reasons.

  • CacheLRU#has(key: String): Boolean: [O(1)] Check whether object exists under key -- without promoting the corresponding object to be the new MRU object.

  • CacheLRU#peek(key: String): Object: [O(1)] Get value of object under key -- without promoting the corresponding object to be the new MRU object. If no object exists under key the value undefined is returned.

  • CacheLRU#touch(key: String): CacheLRU: [O(1)] Touch object under key and this way explicitly promote the object to be the new MRU object. If no object exists under key an exception is thrown.

  • CacheLRU#get(key: String): Object: [O(1)] Get value of object under key -- and promote the object to be the new MRU object. If no object exists under key the value undefined is returned.

  • CacheLRU#set(key: String, val: Object, expires?: Number): CacheLRU: [O(1)] Set value of object under key. If there is already an object stored under key, replace it. Else insert as a new object into the cache. In both cases, promote the affected object to be the new MRU object. If the optional expires parameter is given, it should be the duration in milliseconds the object should maximally last in the cache (it still can be disposed earlier because of LRU purging of the cache). By default this is Infinity.

  • CacheLRU#del(key: String): CacheLRU: [O(1)] Delete object under key. If no object exists under key an exception is thrown.

Convenience Operations

  • CacheLRU#clear(): CacheLRU: [O(n)] Delete all objects in the cache.

  • CacheLRU#keys(): String[]: [O(n)] Get the list of keys of all objects in the cache, in MRU to LRU order.

  • CacheLRU#values(): Object[]: [O(n)] Get the list of values of all objects in the cache, in MRU to LRU order.

  • CacheLRU#each(callback: (val: Object, key: String, order: Number) =>, ctx: Object): Object: [O(n)] Iterate over all objects in the cache, in MRU to LRU order, and call the callback function for each object. The function receives the object value, the object key and the iteration order (starting from zero and steadily increasing).

Implementation Notice

Although Cache-LRU is written in ECMAScript 6, it is transpiled to ECMAScript 5 and this way runs in really all(!) current (as of 2016) JavaScript environments, of course.

Internally, Cache-LRU is based on a managing all objects in a two-headed double-linked list of buckets. This way it can achieve the O(1) time complexity in all its main operations, including the automatically happening LRU purging functionality.

License

Copyright (c) 2015-2024 Dr. Ralf S. Engelschall (http://engelschall.com/)

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.