🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

weak-cache-map

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weak-cache-map

tiny lib to store weak referenced units

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

Caching

With the new weakref's, it feels stupid to keep a reference to an cached object, because this way, it will never be deleted in the memory. Working with TTL's and such feel like a hack and not that practical.

Lets (ab)use the garbage collector for caching. What would be best for caching, is a WeakMap with primitive types. With the modern stuff of WeakRef's, that's possible.

Abstract example

import {WeakCache} from 'weak-cache';

// Just so we have some proper typing, e.g. mongoose has this implementation
type DBModel<T> = { findOne(filter: { _id: string }): Promise<T> }; 

// Imagen some abstract super class that represents a repository for a model
abstract class Repository<T> {

    private cache = new WeakCache<string, T>;

    constructor(private dbModel: DBModel) {

    }
    
    async fetch(id: string) {
        // See if the id's model is still in memory, ifso return it
        const cached = this.cache.get(id);
        if (cached) return cached;

        // get a fresh copy of the database model, and put it in the cache
        const fresh = await this.dbModel.findOne({_id: id});
        if (fresh) this.cache.set(id, fresh);
        return fresh;
    }

    // Silly example it can loop over all weak references
    async getAllModelsInMemory() {
        return Array.from(this.cache.values());
    }

}

Keywords

weakref

FAQs

Package last updated on 29 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts