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 🙏

© 2026 – Pkg Stats / Ryan Hefner

sprage

v1.2.0

Published

一款基于localstorage的轻量级本地存储工具

Readme

sprage.js

一款基于localstorage的轻量级本地存储工具

  • 支持批量存储
  • 支持序列化
  • 支持设置过期时间
  • 支持多种日期格式
  • 支持定义使用次数
  • 可检测占用空间和剩余空间
  • 可自动清除空间
  • 时间戳处理可自定义插件

安装

使用npm安装

$ npm install sprage

使用yarn安装

$ yarn add sprage

引入

使用require引入:

    cosnt Sprage=require('sprage');
    let sprage=new Sprage();

使用import引入:

    import Sprage from 'sprage'
    let sprage=new Sprage()

使用cdn引入

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Sprage.min.js"></script>

基本用法

        let sprage= new Sprage()
        // 设置存储的键值对
        sprage.set('foo', 'boo')
        console.log(sprage.get('foo')) //'boo'

        // 可以用对象的形式进行批量存储
        sprage.set({
            name: 'strage',
            age: '20'
        })
        console.log(sprage.get('name')) //strage
        console.log(sprage.get('age')) //'20'

        // 可以存储对象
        sprage.set(
            'msg', {
                title: 'ts',
                num: 12
            }
        )

        console.log(sprage.get('msg')) //{title: 'ts',num: 12}

        // 检验是否存入了某个值
        console.log(sprage.has('foo')) //true

        // 批量删除
       sprage.remove(['foo', 'name', 'age'])
        console.log(sprage.get('foo')) //null

        // 设置过期时间
       sprage.setTime({
            time:'24h'
        },'30min')//30分钟后过期

        sprage.setTime({
            time:'24h'
        },'2022-3-12')//指定过期的日期

        sprage.setTime({
            time:'24h'
        },new Date().getTime()+10000)//传入指定的时间戳

        //设置可使用次数
        sprage.setCount({count:5},3) //取出三次后失效

        //或者可以使用语法糖
        sprage.setOnce({count:1})//取出一次后失效

        //清除全部缓存
        sprage.clear()

        //获取全部缓存
        sprage.getAll()//获取到的是一个对象数组,每个对象是一个存入的键值对

        //对所有存入的键值对进行操作
        store.forEach((key,val)=>{
            // key,val分别是每一个存入的键和值
        })
        // 检测已经占用的空间和剩余空间
        console.log(sprage.size())   //已经使用117.00KB
        console.log(sprage.surplus()) //剩余空间4943.00KB

自动清除内存占用

一般来说,为了放在误删重要的存储信息,这个功能是默认关闭的,但如果你在开发中需要存储比较大的信息且需要动态分配存储空间,那么这个功能是一个很好的选择,sprage会给根据存储时间来删除那些更早存储的信息,如果你有一些重要的信息需要保留,可以通过设置exclude保留他们

 let sprage = new Sprage({
        autoClear: true,    //开启自动清除缓存
        exclude: ['only']   //设置不会被删除的键值
    })
    const char = "0";
    let count = (9 * 1024 * 1024 / 2);
    let content = new Array(count).fill(char).join("");
    sprage.set('only', '1')
    sprage.set('long', content) //这个信息的长度会占用很大的空间
    sprage.set('long2', content) //存储这个信息时,内存空间已经不够用了,此时会清除那些不在exclude中的数据
    sprage.getAll()//[{only: '"1"'}{long2: '00000000...'}]

sprage清除缓存是按键值对清除的,并不会对localstrage进行扩容。sprage其内部会按存入时间顺序不断删除已有的键值对,直到剩余的存储空间足够存储需要存储的信息

使用插件

sprage的时间戳处理采用了内置的插件,足够适配大多数场景,但如果有特殊需求,可以使用自定义的插件来实现更灵活的时间戳处理

 Sprage.install("time",/*plugins*/ )

插件是一个函数,传入的参数是接收到的日期参数,返回值应该是一个数值类型的时间戳

兼容性

浏览器兼容情况

  • IE 8.0+
  • Chrome 4.0+
  • Firefox 3.0+
  • Opera 10.5+

移动端兼容情况

  • IPhone 2.0+
  • Android 2.0+

源码

源码已放在gitee,供大家学习交流

https://gitee.com/yan-taomeng/sprage.js