微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

StorageLRU LRU 缓存实现

程序名称:StorageLRU

授权协议: BSD

操作系统: 跨平台

开发语言: JavaScript

StorageLRU 介绍

StorageLRU(storage-lru) 是 LRU 缓存实现,可以用在本地存储或者其他存储机制,支持一个类似的接口。

注意: 这个库是使用 Commonjs 风格编写的,如果要在浏览器使用,需要使用
Browserify
Webpack 类似的工具。

主要特性:

使用:

var StorageLRU = require('storage-lru').StorageLRU;
var asyncify = require('storage-lru').asyncify;
var lru = new StorageLRU(asyncify(localStorage), {
    purgeFactor: 0.5,  // this controls amount of extra space to purge.
    purgedFn: function (purgedKeys) {
        console.log('These keys were purged:', purgedKeys);
    }
});
console.log(lru.numItems()); // output 0, assuming the storage is clear
lru.setItem('foo', 'bar', {}, function (err) {
    if (err) {
        // something went wrong. Item not saved.
        console.log('Failed to save item: err=', err);
    }
});
lru.setItem('foojsON', {foo: 'bar'}, {json: true}, function (err) {
    if (err) {
        // something went wrong. Item not saved.
        console.log('Failed to save item: err=', err);
    }
});
lru.getItem('foo', {json: false}, function (err, value) {
    if (err) {
        // something went wrong, for example, can't deserialize
        console.log('Failed to fetch item: err=', err);
        return;
    }
    console.log('The value of "foo" is: ', value);
});
lru.removeItem('foo', function (err) {
    if (err) {
        // something went wrong. Item not removed.
    }
});
var stats = lru.stats();

StorageLRU 官网

https://github.com/yahoo/storage-lru

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐