12 Star 29 Fork 3

罪. / cache-bucket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Introduction

Cache your data with TTL. Using this package in node.js and browser.

Installation

By npm

npm install cache-bucket

Or by yarn

yarn add cache-bucket

Support

Node.js

FileCache and MemoryCache is enable.

Notice that CacheFile will touch a file as storage. The default file path is: ./.filecache

// Based on file
import {cache} from 'cache-bucket/file-cache';

// Based on memory
import {cache} from 'cache-bucket/memory-cache';

Browser

LocalCache and SessionCache and MemoryCache is enable.

Notice that LocalCache is based on localStorage, and SessionCache is based on sessionStorage.

// Based on memory
import {cache} from 'cache-bucket/memory-cache';

// Based on localStorage
import {cache} from 'cache-bucket/local-cache';

// Based on sessionStorage
import {cache} from 'cache-bucket/session-cache';

Methods

get(key: string, defaultValue?: any) => any

Get your cache by key.

If cache is empty, defaultValue will be used. If parameter defaultValue is missing, method will respond null.

cache.get('foo'); // null
cache.get('foo', 'default-bar'); // default-bar

set(key: string, value: any, duration?: number) => void

Set cache data.

The parameter value type can be string, number, object, array. Cache data will expired after millSeconds when you provide duration.

cache.set('foo', 'bar');
cache.set('obj', {pkg: 'cache-bucket'});

// Expired after 3 second.
cache.set('array', ['cache', 'bucket'], 3000);

getOrSet(key: string, onEmpty: () => any, duration?: number) => any

Get cache data.

When cache data is missing, method will set data immediately. And then return it.

cache.getOrSet('foo', () => {
  return 'bar';
}); // bar

add(key: string, value: any, duration?: number) => boolean

Set cache data when key is not exist.

cache.add('foo', 'bar'); // true
cache.add('foo', 'new-bar'); // false

remove(key: string) => void

Delete a cache data.

cache.remove('foo');

clearExpired() => void

Clear all expired cache data

cache.clearExpired();

clearAll() => void

Clear all data.

cache.clearAll();

Advanced

Multiply instances.

import {MemoryCache} from 'cache-bucket/memory-cache';

const cache = new MemoryCache();

cache.set('foo', 'bar');
cache.get('foo'); // bar
import {FileCache} from 'cache-bucket/file-cache';

const cache = new FileCache('./.new-cachefile');

cache.set('foo', 'bar');
cache.get('foo'); // bar

Config

You can choose the file you want to put data when Using FileCache. Just creating config file cache-bucekt.json in project's root directory.

{
  "defaultFilePath": "./.custom-cache-file"
}
MIT License Copyright (c) 2018 原罪 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

轻量缓存系统,可同时在browser和node.js服务端中使用。支持过期时间,缓存锁等功能。 展开 收起
TypeScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/geekact/cache-bucket.git
git@gitee.com:geekact/cache-bucket.git
geekact
cache-bucket
cache-bucket
master

搜索帮助