123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- import { EXPIRE } from '../config/app';
- class Cache {
-
- constructor(handler) {
- this.cacheSetHandler = uni.setStorageSync;
- this.cacheGetHandler = uni.getStorageSync;
- this.cacheClearHandler = uni.removeStorageSync;
- this.cacheExpire = '_expire_2019_12_17_18_44';
- this.name = 'storage';
- }
-
-
- time()
- {
- return Math.round(new Date() / 1000);
- }
-
-
- strTotime(expiresTime){
- let expires_time = expiresTime.substring(0, 19);
- expires_time = expires_time.replace(/-/g, '/');
- return Math.round(new Date(expires_time).getTime() / 1000);
- }
-
- setExpireCaheTag(key, expire) {
- expire = expire !== undefined ? expire : EXPIRE;
- if (typeof expire === 'number') {
- let tag = this.cacheGetHandler(this.cacheExpire), newTag = [],newKeys = [];
- if (typeof tag === 'object' && tag.length) {
- newTag = tag.map(item => {
- newKeys.push(item.key);
- if (item.key === key) {
- item.expire = expire === 0 ? 0 : this.time() + expire;
- }
- return item;
- });
- }
- if (!newKeys.length || newKeys.indexOf(key) === -1) {
- newTag.push({
- key: key,
- expire: expire === 0 ? 0 : this.time() + expire
- });
- }
- this.cacheSetHandler(this.cacheExpire, newTag);
- }
- }
-
-
- setItem(params){
- let obj = {
- name:'',
- value:'',
- expires:"",
- startTime:new Date().getTime()
- }
- let options = {};
-
- Object.assign(options,obj,params);
- if(options.expires){
-
-
-
- uni.setStorageSync(options.name,JSON.stringify(options));
- }else{
-
- let type = Object.prototype.toString.call(options.value);
-
- if(Object.prototype.toString.call(options.value) == '[object Object]'){
- options.value = JSON.stringify(options.value);
- }
- if(Object.prototype.toString.call(options.value) == '[object Array]'){
- options.value = JSON.stringify(options.value);
- }
-
- uni.setStorageSync(options.name,options.value);
- }
- }
-
-
- getExpireCahe(key,$bool)
- {
- try{
- let time = this.cacheGetHandler(key + this.cacheExpire);
- if (time) {
- let newTime = parseInt(time);
- if (time && time < this.time() && !Number.isNaN(newTime)) {
- if ($bool === undefined || $bool === true) {
- this.cacheClearHandler(key);
- this.cacheClearHandler(key + this.cacheExpire);
- }
- return false;
- } else
- return true;
- } else {
- return !!this.cacheGetHandler(key);
- }
- }catch(e){
- return false;
- }
- }
-
-
- set(key,data,expire){
- if(typeof data === 'object')
- data = JSON.stringify(data);
- try{
- this.setExpireCaheTag(key,expire);
- return this.cacheSetHandler(key,data);
- }catch(e){
- return false;
- }
- }
-
-
- has(key)
- {
- return this.getExpireCahe(key);
- }
-
-
- get(key,$default,expire){
- try{
- let isBe = this.getExpireCahe(key);
- let data = this.cacheGetHandler(key);
- if (data && isBe) {
- if (typeof $default === 'boolean')
- return JSON.parse(data);
- else
- return data;
- } else {
- if (typeof $default === 'function') {
- let value = $default();
- this.set(key,value,expire);
- return value;
- } else {
- this.set(key,$default,expire);
- return $default;
- }
- }
- }catch(e){
- return null;
- }
- }
-
-
- clear(key)
- {
- try{
- let cahceValue = this.cacheGetHandler(key + this.cacheExpire);
- if(cahceValue)
- this.cacheClearHandler(key + this.cacheExpire);
- return this.cacheClearHandler(key);
- }catch(e){
- return false;
- }
- }
-
-
- clearOverdue()
- {
-
-
-
-
-
-
- }
-
-
- getItem(name){
-
- let item = uni.getStorageSync(name);
-
- try{
- item = JSON.parse(item);
- }catch(error){
-
- item = item;
- }
-
- if(item.startTime){
- let date = new Date().getTime();
-
- if(date - item.startTime > item.expires){
-
-
- uni.removeStorageSync(name);
- return false;
- }else{
-
- return item.value;
- }
- }else{
-
- return item;
- }
- }
- }
- export default new Cache;
|