cacache icon indicating copy to clipboard operation
cacache copied to clipboard

[ISSUE] Explain to me why this is a stupid use of cacache (storing simple key/value pairs with a simplified cacache interface)

Open advename opened this issue 3 years ago • 2 comments

I've been looking for solid, stable and well maintained Node.js file caching libraries. (Un)Fortunately, cacache was the only one that I could find.

Now, having general experience with remote caching, looking first time at cacache documentation was overwhelming. Tarball why? Integrity what? ...

After experimenting a bit around, I believe I figured out the basics and wrote a simplified API around it.

// cache.js
import path from "path";
import cacache from "cacache";

const cachePath = path.join(__dirname, ".cache");

/**
 * Simplified Cache API written on top of cacache
 * This interface makes using cacache bit more straightforward without
 * deeper understanding of caching itself.
 * @see https://github.com/npm/cacache
 */

/**
 * Set or override key/value in the cache
 */
const put = async (key, value) => {
    const writeAction = await cacache.put(
        cachePath,
        key,
        JSON.stringify(value)
    );

    return writeAction;
};

/**
 * Retrieve a value from the cache.
 */
const get = async (key) => {
    const readAction = await cacache.get(cachePath, key);
    /**
     * Returned "data" key contains data as a Buffer object
     * Convert Buffer binary contents to string
     * @see https://nodejs.org/en/knowledge/advanced/buffers/how-to-use-buffers/#reading-from-buffers
     */
    const dataJSON = readAction.data.toString();
    const data = JSON.parse(dataJSON);
    return data;
};

const remove = async (key) => {
    const removeAction = await cacache.rm.entry(cachePath, key, {
        removeFully: true,
    });
    return removeAction;
};

/**
 * Clears the entire cache
 */
const destroy = async () => {
    const destroyAction = await cacache.rm.all(cachePath);
    return destroyAction;
};

export const cache = {
    put,
    get,
    remove,
    destroy,
};

Before putting this in a production environment, convince me why the above simplified interface is a bad idea for storing key/value pairs, where values may be string, numbers or objects with 500 properties?

advename avatar Aug 05 '22 13:08 advename

I have no familiarity with cacache, but am equally overwhelmed and looking for an interface exactly as you describe. Do you use this in production for your own needs?

Would you be interested in also making / including an equally concise & simple cacache initialization? I'm imagining something like:

var cache = cacache({
    location: 'some/folder/or/file',
    name: 'footheflowerhorn',
    duration: 1000 * 3600 * 24,
    persist: true
});

horak avatar Aug 12 '22 18:08 horak

What would be the usage of persist? Cacache is a file based storage?

advename avatar Sep 12 '22 09:09 advename

This module is pretty customized for the needs of the npm cli. It should work just fine for a generic key/value cache even if it is a bit overweight for that.

wraithgar avatar May 02 '23 20:05 wraithgar

@wraithgar thanks for the feedback.

My question was, however, regarding the implementation that I've created. I'm too much of a novice to know or see if I missed something important out.

advename avatar May 04 '23 06:05 advename