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

the-storage

v0.1.1

Published

A storage that automatically removes the least-recently-used items.

Downloads

1

Readme

TheStorage · build npm license

A storage that automatically removes the least-recently-used items.

Features

  • Fast LRU implementation
  • Gzipped version has less than 400 bytes
  • Minimalist API
  • Supports to both Node and Browser
  • Supports to Types
  • Zero-dependency

Installation

To install the-storage, execute:

$ npm install the-storage

or

$ yarn add the-storage

Quickstart

TheStorage is a cache class which is designed to keep storing just the most recently used items. To ensure it, the class receives a capacity, that will be used to discard the least recently used items when the capacity is reached.

import TheStorage from 'the-storage';

// `capacity` is required and it must to be a posite integer
const storage = new TheStorage(3);

storage.setItem('key1', 'value1');
storage.setItem('key2', 'value2');
storage.getItem('key1'); // 'value1'
storage.setItem('key3', 'value3');

// At this point, the item associeted with `key2` will be evicted.
// It happens because the capacity was reached and `key1` was used before `key2`.
storage.setItem('key4', 'value4');

storage.size(); // 3
storage.contains('key2'); // false
storage.contains('key1'); // true

Methods

contains(key: any): boolean Checks if the given key exist within the storage

setItem(key: any, value: any): boolean Adds the key-value pair to the cache if the key is not in the cache yet. Otherwise, if the key exists, updates the value of the key. In case the current number of keys exceeds the capacity, then it evicts the least recently used key that is not marked as persistent.

getItem(key: any): any Retrieves the value associeted with the given key if it exists in the cache. If the key is not in the cache, it returns undefined.

removeItem(key: any): boolean Deletes item and returns true if the item existed in the storage - persistent will be removed as well. Returns false if the element doesn't exist in the storage.

setPersistent(key: any): void Makes item persistent, i.e the item can no longer be automatically evicted. However, the item still will be removed when invoking removeItem or clear.

import TheStorage from 'the-storage';

const storage = new TheStorage(3);
storage.setItem('key1', 'value1');
storage.setPersistent('key1');
storage.setItem('key2', 'value2');
storage.setItem('key3', 'value3');

// At this point the item associeted with `key2` will be evicted
// since the `key1` is marked as `persistent` and can not be evicted.
storage.setItem('key4', 'value4');

storage.contains('key1'); // true
storage.contains('key2'); // false

removePersistent(key: any): void Makes item no longer a persistent item.

togglePersistent(key: any): void Makes item persistent if it was not yet, or otherwise undo the persistent flag.

size(): number Returns the storage size.

clear(): void Remove all items - even the persistent ones - from the cache.

LICENSE

MIT