{"id":17058263,"url":"https://github.com/wuchangming/web-storage-cache","last_synced_at":"2025-04-04T13:11:53.388Z","repository":{"id":1014322,"uuid":"37472998","full_name":"wuchangming/web-storage-cache","owner":"wuchangming","description":"对localStorage 和sessionStorage 进行了扩展，添加了超时时间，序列化方法","archived":false,"fork":false,"pushed_at":"2022-05-25T23:33:56.000Z","size":340,"stargazers_count":657,"open_issues_count":20,"forks_count":161,"subscribers_count":31,"default_branch":"master","last_synced_at":"2024-10-30T08:17:15.339Z","etag":null,"topics":["expiration","localstorage","sessionstorage"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wuchangming.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-15T15:15:35.000Z","updated_at":"2024-10-28T06:33:51.000Z","dependencies_parsed_at":"2022-08-07T01:00:02.113Z","dependency_job_id":null,"html_url":"https://github.com/wuchangming/web-storage-cache","commit_stats":null,"previous_names":["wqteam/web-storage-cache"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuchangming%2Fweb-storage-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuchangming%2Fweb-storage-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuchangming%2Fweb-storage-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuchangming%2Fweb-storage-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wuchangming","download_url":"https://codeload.github.com/wuchangming/web-storage-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790939,"owners_count":20348378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expiration","localstorage","sessionstorage"],"created_at":"2024-10-14T10:29:13.557Z","updated_at":"2025-03-21T11:11:39.071Z","avatar_url":"https://github.com/wuchangming.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# WebStorageCache  \n[![Build Status](https://travis-ci.org/wuchangming/web-storage-cache.svg?branch=master)](https://travis-ci.org/wuchangming/web-storage-cache)\n[![npm](https://img.shields.io/npm/dt/web-storage-cache.svg)](https://www.npmjs.com/package/web-storage-cache)\n\u003ca href='https://gitter.im/wuchangming/web-storage-cache'\u003e\n\u003cimg src='https://badges.gitter.im/Join%20Chat.svg' alt='Gitter Chat' /\u003e\n\u003c/a\u003e\n\n### Language\n see [English Document](https://github.com/wuchangming/web-storage-cache/blob/master/README_en.md)\n\n`WebStorageCache` 对HTML5 `localStorage` `和sessionStorage` 进行了扩展，添加了超时时间，序列化方法。可以直接存储json对象，同时可以非常简单的进行超时时间的设置。  \n\u003cb\u003e优化\u003c/b\u003e：`WebStorageCache`自动清除访问的过期数据，避免了过期数据的累积。另外也提供了清除全部过期数据的方法：`wsCache.deleteAllExpires();`\n\n# 用法\n\n[下载](https://github.com/wuchangming/web-storage-cache/releases) 最新 WebStorageCache。\n\nnpm下载\n```\nnpm install web-storage-cache --save-dev\n```\n\n使用WebStorageCache，只要在页面上引入下面代码即可。\n```html\n\u003cscript src=\"src/web-storage-cache.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n// create WebStorageCache instance.\nvar wsCache = new WebStorageCache();\n// cache 'wqteam' at 'username', expired in 100 seconds\nwsCache.set('username', 'wqteam', {exp : 100});\n\u003c/script\u003e\n```\n也可以在RequireJS使用WebStorageCache：\n```javascript\ndefine(['web-storage-cache'], function(WebStorageCache) {\n    // 初始化 WebStorageCache 实例.\n    var wsCache = new WebStorageCache();\n    // 缓存字符串'wqteam' 到 'username' 中, 超时时间100秒.\n    wsCache.set('username', 'wqteam', {exp : 100});\n});\n```\n\n## 例子\n```javascript\nvar wsCache = new WebStorageCache();\n\n// 缓存字符串'wqteam' 到 'username' 中, 超时时间100秒\nwsCache.set('username', 'wqteam', {exp : 100});\n\n// 超时截止日期，可用使用Date类型\nvar nextYear = new Date();\nnextYear.setFullYear(nextYear.getFullYear() + 1);\nwsCache.set('username', 'wqteam', {exp : nextYear});\n\n// 获取缓存中 'username' 的值\nwsCache.get('username');\n\n// 缓存简单js对象，默认使用序列化方法为JSON.stringify。可以通过初始化wsCache的时候配置serializer.serialize\nwsCache.set('user', { name: 'Wu', organization: 'wqteam'});\n\n// 读取缓存中的简单js对象 - 默认使用反序列化方法为JSON.parse。可以通过初始化wsCache的时候配置serializer.deserialize\nvar user = wsCache.get('user');\nalert(user.name + ' belongs to ' + user.organization);\n\n// 删除缓存中 'username'\nwsCache.delete('username');\n\n// 手工删除所有超时CacheItem,\nwsCache.deleteAllExpires();\n\n// 清除客户端中所有缓存\nwsCache.clear();\n\n// 为已存在的（未超时的）缓存值设置新的超时时间。\nwsCache.touch('username', 1);\n\n// 如果缓存中没有key为username2的缓存，则添加username2。反之什么都不做\nwsCache.add('username2', 'wqteam', {exp : 1});\n\n// 如果缓存中有key为username的缓存，则替换为新值。反之什么都不做\nwsCache.replace('username', 'new wqteam', {exp : 1});\n\n// 检查当前选择作为缓存的storage是否被用户浏览器支持。\n//如果不支持调用WebStorageCache API提供的方法将什么都不做。\nwsCache.isSupported();\n\n```\n# API\n\n## Constructor\n```javascript\nvar wsCache = new WebStorageCache({\n    // [可选] 'localStorage', 'sessionStorage', window.localStorage, window.sessionStorage\n    //        或者其他实现了 [Storage API] 的storage实例.\n    //        默认 'localStorage'.\n    storage: 'localStorage',\n    // [可选]  类型Number，公共超时事件设置。默认无限大\n    exp: Infinity\n});\n```\n## isSupported\n检查当前选择作为缓存的storage是否被用户浏览器支持。\n如果不支持调用WebStorageCache API提供的方法将什么都不做。\n```javascript\nwsCache.isSupported(); // 返回值Boolean。\n```\n## set\n往缓存中插入数据。\n```javascript\n// key [必填] 必须要为String类型。\n// value [必填] 支持所以可以JSON.parse 的类型。注：当为undefined的时候会执行 delete(key)操作。\n// options [选填] js对象，包含两个属性 exp 和 force。\n// {\n//     // 类型Number。超时时间，秒。默认无限大。\n//     exp: 100,\n//     // 类型Boolean。为true时：当超过最大容量导致无法继续插入数据操作时，先清空缓存中已超时的\n//     // 内容后再尝试插入数据操作。默认为true。\n//     force: true\n// }\nwsCache.set(key, value, options);\n```\n## get\n根据key获取缓存中未超时数据。返回相应类型String、Boolean、PlainObject、Array的值。\n```javascript\n// key [必填] String类型。如果发现该key对应的值已过期,会进行delete(key)操作，返回null。\nwsCache.get(key);\n```\n## delete\n根据key删除缓存中的值。\n```javascript\nwsCache.delete(key);\n```\n## deleteAllExpires\n删除缓存中所有通过WebStorageCache存储的超时值。\n```javascript\nwsCache.deleteAllExpires();\n```\n## clear\n清空缓存中全部的值。注意：这个方法会清除不是使用WebStorageCache插入的值。推荐使用:`deleteAllExpires`。\n```javascript\nwsCache.clear();\n```\n## touch\n根据key为已存在的（未超时的）缓存值以当前时间为基准设置新的超时时间。\n```javascript\n// key [必填] String类型\n// exp [必填] number 单位：秒 js对象包含exp属性（以当前时间为起点的新的超时时间）\nwsCache.touch(key, exp: 1);\n```\n## add\n根据key做插入操作，如果key对应的值不存在或者已超时则插入该值，反之什么都不做。\n注：不是通过WebStorageCache插入的值也会当作失效的值，依然执行`add`操作\n```javascript\nwsCache.add(key, value, options);\n```\n## replace\n根据key做插入操作，如果key对应的值存在并且未超时则插入该值，反之什么都不做  \n注：超时时间以当前时间为基准重新设置。\n```javascript\nwsCache.replace(key, value, options);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwuchangming%2Fweb-storage-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwuchangming%2Fweb-storage-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwuchangming%2Fweb-storage-cache/lists"}