博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
内存缓存memory-cache
阅读量:4456 次
发布时间:2019-06-08

本文共 2820 字,大约阅读时间需要 9 分钟。

 

A simple in-memory cache for node.js

 

Installation

npm install memory-cache --save

 

Usage

var cache = require('memory-cache'); // now just use the cache  cache.put('foo', 'bar');console.log(cache.get('foo')); // that wasn't too interesting, here's the good part  cache.put('houdini', 'disappear', 100, function(key, value) {    console.log(key + ' did ' + value);}); // Time in ms  console.log('Houdini will now ' + cache.get('houdini')); setTimeout(function() {    console.log('Houdini is ' + cache.get('houdini'));}, 200);  // create new cache instance var newCache = new cache.Cache(); newCache.put('foo', 'newbaz'); setTimeout(function() {  console.log('foo in old cache is ' + cache.get('foo'));  console.log('foo in new cache is ' + newCache.get('foo'));}, 200);

 

which should print

barHoudini will now disappearhoudini did disappearHoudini is nullfoo in old cache is bazfoo in new cache is newbaz

 

API

put = function(key, value, time, timeoutCallback)

  • Simply stores a value
  • If time isn't passed in, it is stored forever
  • Will actually remove the value in the specified time in ms (via setTimeout)
  • timeoutCallback is optional function fired after entry has expired with key and value passed (function(key, value) {})
  • Returns the cached value

get = function(key)

  • Retrieves a value for a given key
  • If value isn't cached, returns null

del = function(key)

  • Deletes a key, returns a boolean specifying whether or not the key was deleted

clear = function()

  • Deletes all keys

size = function()

  • Returns the current number of entries in the cache

memsize = function()

  • Returns the number of entries taking up space in the cache
  • Will usually == size() unless a setTimeout removal went wrong

debug = function(bool)

  • Turns on or off debugging

hits = function()

  • Returns the number of cache hits (only monitored in debug mode)

misses = function()

  • Returns the number of cache misses (only monitored in debug mode)

keys = function()

  • Returns all the cache keys

exportJson = function()

  • Returns a JSON string representing all the cache data
  • Any timeoutCallbacks will be ignored

importJson = function(json: string, options: { skipDuplicates: boolean })

  • Merges all the data from a previous call to export into the cache
  • Any existing entries before an import will remain in the cache
  • Any duplicate keys will be overwritten, unless skipDuplicates is true
  • Any entries that would have expired since being exported will expire upon being imported (but their callbacks will not be invoked)
  • Available options:
    • skipDuplicates: If true, any duplicate keys will be ignored when importing them. Defaults to false.
  • Returns the new size of the cache

Cache = function()

  • Cache constructor
  • note that require('cache') would return the default instance of Cache
  • while require('cache').Cache is the actual class

转载于:https://www.cnblogs.com/BlingSun/p/7890074.html

你可能感兴趣的文章
UVA10791
查看>>
P2664 树上游戏
查看>>
jQuery 停止动画
查看>>
Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
查看>>
MyBatis Generator去掉生成的注解
查看>>
教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
查看>>
lua连续随机数
查看>>
checkstyle使用介绍
查看>>
history.js 一个无刷新就可改变浏览器栏地址的插件(不依赖jquery)
查看>>
会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!
查看>>
二维码图片生成
查看>>
在做操作系统实验的一些疑问
查看>>
Log4J日志配置详解
查看>>
NameNode 与 SecondaryNameNode 的工作机制
查看>>
Code obfuscation
查看>>
大厂资深面试官 带你破解Android高级面试
查看>>
node.js系列(实例):原生node.js实现接收前台post请求提交数据
查看>>
SignalR主动通知订阅者示例
查看>>
golang的表格驱动测试
查看>>
用python实现矩阵转置
查看>>