我如何在打字稿中使用内存缓存模块?

问题描述 投票:0回答:1

我正在努力在打字稿中实现内存中缓存功能。我使用memcache包npm install memcache安顿下来。我找不到@ types / memcache。

当我尝试在ts文件中使用时,出现以下错误:

    src/util/cache-data-store.ts:1:22 - error TS7016: Could not find a declaration file for module 'memcache'. '/Users/reza.razavipour/sandbox/gsm-unlock/node_modules/memcache/lib/memcache.js' implicitly has an 'any' type.
  Try `npm install @types/memcache` if it exists or add a new declaration (.d.ts) file containing `declare module 'memcache';`

1 import memcache from 'memcache';

Found 1 error.

如何解决此错误?

还有其他使用更广泛的打字机内存缓存吗?

typescript memcached
1个回答
0
投票

您可以使用内存缓存https://www.npmjs.com/package/memory-cache它支持Typescript,这是您可以使用的方式:

安装

yarn add memory-cache
yarn add -D @types/memory-cache

用法

import memoryCache, { CacheClass } from 'memory-cache';

const memCache: CacheClass<string, string> = new memoryCache.Cache();

// Store a value
memCache.put('MyKey', 'My Value', duration * 1000);

// Retrieve the value
memCache.get('MyKey');
© www.soinside.com 2019 - 2024. All rights reserved.