npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cache-lib

v0.0.6

Published

cache.js 是一个轻量级的 JS 库,对 `localStorage`、`sessionStorage`进行了扩展,增加了序列化方法和过期时间。可以直接存取JSON对象、设置过期时间。

Downloads

12

Readme

cache.js

Build Status npm

cache.js 是一个轻量级的 JS 库,对 localStoragesessionStorage进行了扩展,增加了序列化方法和过期时间。可以直接存取JSON对象、设置过期时间。

API 结构如下:

cache.doSomething([key], [value], [expire])  // 部分 API 无需传入参数

开始

# 直接引用

下载 最新的 cache.js ,直接通过 script 标签在 html 页面引用:

<script src="cache.js"></script>

# npm

在命令行工具里执行下面这个命令:

$ npm install cache-lib --save-dev

# RequireJS

RequireJS 里使用:

define(['cache'], function(Cache){
    var cache = new Cache();  // 初始化 cahce 实例
    /* ... */
})

# CDN

我们推荐链接到一个你可以手动更新的指定版本号:

<script src="https://unpkg.com/[email protected]/dist/cache.js"></script>

使用压缩版本,这是一个更小的构建,可以带来比开发环境下更快的速度体验。

<script src="https://unpkg.com/[email protected]/dist/cache.min.js"></script>

API

使用之前需要初始化 cache 实例:

var cache = new Cache();

默认是使用 localStorage,可传入参数来指定使用 localStorage 或者 sessionStorage:

var cache = new Cache('localStorage');
// or
var cache = new Cache('sessionStorage');

# .set( key, value, [expire] )

Params:

  • key (String): 需要存储的键名
  • value (Any): 需要存储的值
  • [expire] (Object): 设置过期时间

Examples:

// 把字符串'kyle'存储到'user'里,不设置过期时间则永久有效
cache.set('user', 'kyle');

// 直接存储JSON对象,并设置过期时间为2s后
cache.set('user', {name: 'kyle', age: 28}, {type: 's', delay: 2});

Notes:

  1. 设置过期时间时要传入一个对象,指定类型 type<string>和延迟时间 delay<number>,类型参考如下:

| Type | Description | Example | | :--- | :---------- | ------------------------------------------------- | | y | 年 | {type: 'y', delay: 1} // 设置过期时间为1年后 | | M | 月 | {type: 'M', delay: 1} // 设置过期时间为1个月后 | | w | 星期 | {type: 'w', delay: 1} // 设置过期时间为1个星期后 | | d | 日 | {type: 'd', delay: 1} // 设置过期时间为1天后 | | h | 小时 | {type: 'h', delay: 1} // 设置过期时间为1个小时后 | | m | 分钟 | {type: 'm', delay: 1} // 设置过期时间为1分钟后 | | s | 秒 | {type: 's', delay: 1} // 设置过期时间为1秒钟后 |

  1. 当传入的类型不在上表范围内时,则默认 type 为 'd';
  2. 如果 set 的时候 key 已经存在,并且设置了过期时间,当再次设置过期时间时则覆盖原值,此次 set 时没有设置过期时间则保持此前设置的时间不变;
  3. 如果 set 的时候 key 已经存在,并且设置的时间已经过期,不会清除该条数据而是覆盖新值;

# .get( key )

Params:

  • key (String): 需要获取的键名

Examples:

// 获取'user'下存储的值
cache.get('user');

// 如存储的是对象时可直接使用
cache.get('user').name;
cache.get('user').age;

Notes:

  1. 如果 get 的这个 key 设置的时间已经过期则返回之前存储的数据,并清除该条数据;
  2. 如果 get 的这个 key 不存在则返回 undefined;

# .update( key, [value], expire )

Params:

  • key (String): 需要更新的键名
  • [value] (Any): 需要存储的值
  • expire (Object): 设置过期时间

Examples:

// 更新过期时间为当前时间之后5s
cache.update('user', {type: 's', delay: 5});

// 同时更新存储的数据和过期时间
cache.update('user', {name: 'kyle', age: 28}, {type: 's', delay: 2});

Notes:

  1. 如果 update 的这个 key 不存在则返回 undefined;
  2. 只更新过期时间则传入2个参数,第2个参数为过期时间,过期时间设置方法同set,请参考set 的例子;
  3. 当同时更新存储的数据和过期时间则传入3个参数,第2个参数为更新的数据,第3个参数为过期时间;(与set传入3个参数时效果相同)
  4. 只需更新存储的数据时请使用set方法;

# .remove( key )

Params:

  • key (String): 需要移除的键名

Examples:

// 移除'user'
cache.remove('user');

Notes:

  1. 如果 remove 的这个 key 不存在则返回 undefined;

# .clear( ['exp'] )

Params:

  • ['exp'] (String): 传入字符串'exp'来选择过期数据

Examples:

// 清除所有数据
cache.clear();

// 清除过期的数据
cache.clear('exp');

Notes:

  1. 此方法会移除所有包括不是通过cache.js存入的数据;
  2. 必须传入'exp'才能清除过期的数据,若不是则清除所有数据;

# .keys( ['exp'] )

Params:

  • ['exp'] (String): 传入字符串'exp'来选择过期数据

Examples:

// 获取所有数据的键名
cache.keys(); 

// 获取过期的数据的键名
cache.keys('exp');

Notes:

  1. 此方法会返回所有包括不是通过cache.js存入的数据的键名;
  2. 必须传入'exp'才能返回过期的数据的键名,若不是则返回所有数据的键名;

# .debug

cache 提供了详细的 console 输出,默认禁用。

启用如下设置即可:

cache.debug.enable();

在浏览器的控制台里可以看到如下输出:

console.log

禁用如下设置即可:

cache.debug.disabled();