@nazarkulyk/pta-kyma-cache
v1.0.3
Published
## Deploy redis to kyma
Downloads
3
Readme
redis-example
Deploy redis to kyma
cd deploy
kubectl apply -f redis.yaml
kubectl apply -f redis-commander.yaml
Redis Commander
Create API Rule for redis commander, than you will have GUI Access to your redis storage
Create function
{
"name": "redis-example",
"version": "1.0.0",
"dependencies": {
"redis": "^3.0.2",
"nanoid": "^3.1.12",
"lodash": "^4.17.20"
}
}
const redis = require('redis');
const { nanoid } = require('nanoid');
const { pick, omit } = require('lodash');
const { promisify } = require("util");
/**
* Use it like that:
*
* get Redis Service Info:
* https://redis-demo.c-9a36b66.kyma.shoot.live.k8s-hana.ondemand.com/info
*
* protocol some data from request:
*
*/
const REQUEST_DATA_LIST = ['path', 'query', 'url', 'body'];
class RedisStorage {
storage = undefined;
asyncGet = void 0;
asyncKeys = void 0;
asyncSet = void 0;
asyncInfo = void 0;
constructor(options) {
this.storage = redis.createClient(options);
this.asyncGet = promisify(this.storage.get).bind(this.storage);
this.asyncKeys = promisify(this.storage.keys).bind(this.storage);
this.asyncSet = promisify(this.storage.set).bind(this.storage);
this.asyncInfo= promisify(this.storage.info).bind(this.storage);
}
async getAll() {
let values = [];
const keys = await this.asyncKeys("*");
for (const key of keys) {
const value = await this.asyncGet(key);
values.push(JSON.parse(value));
}
return values;
}
async info() {
return this.asyncInfo();
}
async set(data = {}) {
if (!data.id) {
data.id = nanoid();
}
const value = await this.asyncGet(data.id);
if (value) {
throw new Error('Object already exists');
}
await this.asyncSet(data.id, JSON.stringify(omit(data, 'id')));
return data.id;
}
}
module.exports = {
main: async (event, context) => {
const path = event.extensions.request.path;
let storage;
try {
storage = new RedisStorage({host: process.env.REDIS_HOST, port: process.env.REDIS_PORT});
} catch(e) {
console.error(e);
throw new Error(e);
}
if(path === "/info") {
return storage.info()
} else if(path === "/set") {
return storage.set({ path, date: new Date(), request: pick(event.extensions.request, REQUEST_DATA_LIST)});
} else if(path === "/get") {
return storage.getAll();
} else {
return "provide some operator";
}
}
}