Nebulous Puffy Marshmallows

    memlim
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.0 • Public • Published

    memlim

    npm CircleCI Codecov

    memlim is a tiny in-memory cache library that can be used on limited size of memory.

    Installation

    $ npm install memlim
    

    Usage

     
    import {Memlim} from "memlim"
     
    const memlim = new Memlim(1000 * 1000 * 10); // 10MB cache at the most
     
    memlim.put("a", "aaa"); // put cache with no ttl
    memlim.put("b", "bbb", 1000); // with ttl (msec)
    memlim.put("bindata", new ArrayBuffer(100)); // binary data
     
    memlim.get("a"); // get data;
     
    memlim.delete("a"); // delete
     
    memlim.freeSize; // total free size
    memlim.usedSize; // total used size 
    memlim.dataCount; // total data count
     
    memlim.clear(); // clear all data

    Overwrite Policies

    Memlim will try to reuse space for new data when total used size exceeded the limitation.
    By default, memlim deletes data which was accessed (by put/get) at the oldest date.

    This policy can be changed by passing the policy value to constructor.

    const overwrite = "___"; // set overwrite policy
    const memlim = new Memlim(100, {overwrite})

    "oldestAccess"

    default policy. Try to delete entries that was accessed on the oldest date.

    "oldest"

    Try to delete entries that was put on the oldest date.

    "minSize"

    Try to delete entries that has the minimum size in all data.

    "maxSize"

    Try to delete entries that has the maximum size in all data.

    "clear"

    custom compare function

    You can pass arbitrary compare function between entries.
    When compare function was given, memlim will sort entries by that and delete the first entry recursively until total free size exceeds size to be added.

    const memlim = new Memlim(12, {
        overwrite: (a, b) => a.data.localeCompare(b.data)
    });
    memlim.put("a", "11");
    memlim.put("b", "22");
    memlim.put("c", "01");
    // will delete c("01")
    memlim.put("d", "33");
     

    entry's type schema is as below:

    type MemlimCacheType = string | ArrayBuffer;
    export type MemlimEntry<T extends MemlimCacheType> = {
        key: string,
        data: T,
        size: number,
        lastAccessedAt: Date,
        createdAt: Date
    }

    About size

    memlim only limits cache's content size. This means that key and associated JS object's sizes are not measured and not included in memlim's size. Be careful to determine size to use for caching.

    LICENSE

    MIT

    Install

    npm i memlim

    DownloadsWeekly Downloads

    96

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    142 kB

    Total Files

    10

    Last publish

    Collaborators

    • keroxp