12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- 'use strict';
- const Service = require('egg').Service;
- class CacheService extends Service {
- async setString(key, value, seconds) {
- let {redis} = this.app;
- if( !seconds ) return await redis.set(key,value);
- return await redis.set(key,value,'EX',seconds);
- }
- async getString( key ) {
- let {redis} = this.app;
- return await redis.get(key);
- }
- async setJSON(key,value,seconds) {
- let {redis} = this.app;
- value = JSON.stringify(value);
- if(!seconds) return await redis.set(key,value);
-
- return await redis.set(key,value,'EX',seconds);
- }
- async getJSON(key){
- let {redis} = this.app;
- let data = await redis.get(key);
- if( !data ) return;
- return JSON.parse(data);
- }
- async removeCache(key) {
- let {redis} = this.app;
- let ret = await redis.del( key );
- console.log("removeCache-> " + key, ret);
- }
- }
- module.exports = CacheService;
|