rolandstarke / node-cache-manager-fs-hash

Node.js Cache Manager store for filesystem that saves the data in a file named with the hash of the key
MIT License
38 stars 10 forks source link

store.get is not a function #16

Closed sayjeyhi closed 7 months ago

sayjeyhi commented 7 months ago

Hi

I am using cache-manager : "^5.5.1", and I get an error when I try to use node-cache-manager-fs-hash

 ⨯ ../node_modules/cache-manager/dist/caching.js (77:34) @ get
 ⨯ TypeError: store.get is not a function

It seems the package is not compatible with the new versions of cache-manager.

here is my code:

import { caching } from 'cache-manager';
import fsStore from 'cache-manager-fs-hash';

async function CacheService() {
  const cacheService = await caching({
    options: {
      path: 'diskcache',
      // time to life in seconds
      subdirs: true,
      // path for cached files
      ttl: 60 * 60, // create subdirectories to reduce the
      // files in a single dir (default: false)
      zip: true, // zip files to save diskspace (default: false)
    },
    store: fsStore,
  });

  const get = (key: string) => {
    console.log('CacheService get', cacheService);
    return cacheService.get(key);
  };

  const set = (key: string, value: any) => {
    return cacheService.set(key, value);
  };

  return {
    get,
    set,
  };
}

export default CacheService;
lennyhans commented 7 months ago

Probably is related to this PR, which seems to address the problem

If you check, the current master exports a create method instead of the store itself. Can you try use that as the store?

import { create as fsStore } from 'cache-manager-fs-hash';
const cacheService = await caching({
    options: {
      // ... options
    },
    store: fsStore,
  });
rolandstarke commented 7 months ago

Based on the source code https://github.com/jaredwray/cache-manager/blob/v5.5.0/src/caching.ts#L69 it looks like the caching method changed, there is no store option anymore.

I think the new way would be to pass the store object as the first param like its done in https://github.com/huijiewei/cache-manager-sqlite

const memStoreCache = await cacheManager.caching(sqliteStore({cacheTableName: 'caches'}));
rolandstarke commented 7 months ago

I published version 2 of the node-cache-manager-fs-hash library and updated the Readme examples. The library should now work with cache-manager version 5.