Skip to content

Instantly share code, notes, and snippets.

@sanchezzzhak
Created June 3, 2019 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanchezzzhak/1b791bba390c9dab7d051a3351355cb9 to your computer and use it in GitHub Desktop.
Save sanchezzzhak/1b791bba390c9dab7d051a3351355cb9 to your computer and use it in GitHub Desktop.
const Memcached = require('memcached');
module.exports = ({keyComponent: componentName = 'memcache', config: config} = {}) => ({
settings: {
[componentName]: config,
},
created() {
this[componentName] = new Memcached(this.settings[componentName]);
// this[componentName].TIME_MAX = 2592000;
// this[componentName].TIME_DAY = 86400;
// this[componentName].TIME_HOUR = 3600;
},
started() {},
stopped() {},
methods: {
setCache(key, val, expTime = 3600 ) {
let self = this;
return new Promise((resolve, reject) => {
self[componentName].set(key, val, expTime, (err) => {
if (err) {
reject(err);
} else {
resolve(null);
}
});
});
},
removeCache(key) {
let self = this;
return new Promise((resolve, reject) => {
self[componentName].del(key, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
},
getCache(key) {
let self = this;
return new Promise((resolve, reject) => {
self[componentName].get(key, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment