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

smart-lru

v0.0.3

Published

Simple, fast, smart and flexible Least Recently Used (LRU) cache

Downloads

2

Readme

smart-lru

This project aims to be fast and smart. It currently sets itself apart from existing LRU caches in its ability to evict data from the cache once it fills a given amount of memory, rather than requiring a specific number of allowed cache entries. Cached data can vary in size and it is generally desirable to cache as much data as memory will allow.

Installation

npm install smart-lru

Example Usage

var SmartLRU = require('smart-lru');

var cache = new SmartLRU({
    inactiveTTL: 86400, // Keep unused objects in the cache for 1 day
    maxSize: '80%',     // Don't use more than 80% of the system memory
    maxKeys: 5000       // Don't allow more than 5000 keys in the cache
});

cache.set('test', {someKey: 5});

cache.get('test'); // Returns {someKey: 5}

API

SmartLRU#constructor(options)

Options and defaults

  • inactiveTTL: integer, default 0 (no TTL). The default number of seconds unused cached objects should remain in the cache.
  • maxSize: string, default 50m. The maximum amount of memory the cache should use. Size can be specified as a percentage with as suffix of '%', as a number in bytes, or as a string with a suffix of 'k', 'm' or 'g'. The suffix is not case sensitive.
  • maxKeys: integer, default 0 (unlimited). The maximum number of keys allows in the cache, regardless of whether it has reached maxSize.

SmartLRU#get(key[, touch])

Get the value of the given cache key. Returns undefined if the key is not set. This is important since null is a valid cacheable value.

If touch is passed and set to false it will return the value without resetting its TTL or its place in line for eviction from the cache.

SmartLRU#set(key, value[, ttl])

Set a value in the cache. The value can be anything, but if it is not a Buffer then it will be JSON encoded before being stored. It will be decoded on get so that part is transparent, but it will affect how much memory the value uses. In the future this may be able to measure object sizes properly and store them in their native form. Although, in some cases the JSON encoded value can be smaller.

The ttl is optional and will be defaulted to the inactiveTTL specified in the constructor options if not given. When you use this method it will completely replace any existing entry under the given key and reset its TTL. Passing a value of 0 to the optional ttl parameter will cause the given value to have no TTL even if there is a default inactiveTTL set.

SmartLRU#touch(key)

Reset the ttl and eviction queue position for the given key.

SmartLRU#hasKey(key)

Returns true if the given key exists in the cache, false otherwise. This should be used before a call to update() if you are not completely sure the key exists.

SmartLRU#update(key, value)

Update the value of a key without resetting its TTL or changing its place in line for eviction from the cache. Note that this will throw an exception of the given key does not exist in the cache.

SmartLRU#del(key)

Delete the given cache key.

SmartLRU#reset()

Clear the entire LRU cache.