Skip to content

Instantly share code, notes, and snippets.

@jjdechavez

jjdechavez/index.js

Created Sep 4, 2020
Embed
What would you like to do?
sample of redisearch
const express = require("express");
const fetch = require("node-fetch");
const app = express();
const redis = require("redis");
const redisearch = require("redredisearch");
const client = redis.createClient({ port: 6379, host: "127.0.0.1" });
redisearch.setClient(client);
app.use(express.json());
app.get("/pets", (req, res) => {
redisearch.createSearch("pets", {}, function (err, search) {
if (err) throw err;
let strs = [];
strs.push("Tobi wants four dollars");
strs.push("Tobi only wants $4");
strs.push("Loki is really fat");
strs.forEach(function (str, i) {
search.index(str, i);
});
search.query("Tobi").end(function (err, ids) {
if (err) throw err;
//console.log('Search results for "%s":', query);
console.log(ids);
ids.forEach(function (id) {
console.log("data - %s", strs[id]);
});
});
});
return res.json({ message: "OK" });
});
app.get("/hashes", async (req, res) => {
const hashKey = "test:hashes";
return client.hgetall(hashKey, (err, hash) => {
if (hash) {
console.log(hash);
return res.json({ type: "saved", data: hash });
} else {
client.HMSET(
hashKey,
"hash:1",
"hash:value",
"hash:2",
"hash:cool",
(err, data) => {
return res.json({ type: "created", data });
}
);
}
});
});
app.get("/photos", (req, res) => {
// key to store results in Redis store
const photosRedisKey = "user:photos";
// Try fetching the result from Redis first in case we have it cached
return client.get(photosRedisKey, (err, photos) => {
// If that key exists in Redis store
if (photos) {
let data = JSON.parse(photos);
return res.json({ source: "cache", data, dataLength: data.length });
} else {
// Key does not exist in Redis store
// Fetch directly from remote api
fetch("https://jsonplaceholder.typicode.com/photos")
.then((response) => response.json())
.then((photos) => {
// Save the API response in Redis store, data expire time in 3600 seconds, it means one hour
client.setex(photosRedisKey, 1500, JSON.stringify(photos));
// Send JSON response to client
return res.json({ source: "api", data: photos });
})
.catch((error) => {
// log error message
console.log(error);
// send error to the client
return res.json(error.toString());
});
}
});
});
app.get("/members", (req, res) => {
let setSize = 10;
const memberKey = "members";
while (setSize > 0) {
client.SADD(memberKey, `member:${setSize}`);
setSize--;
}
client
.multi()
.scard("key")
.smembers("key")
.keys("*")
.dbsize()
.exec(function (err, replies) {
console.log("MULTI got " + replies.length + " replies");
replies.forEach(function (reply, index) {
console.log("REPLY @ index " + index + ": " + reply.toString());
});
res.json({ multi: replies });
});
});
app.listen(5000, () => console.log("app running on port 5000"));
{
"name": "l-nredis",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "^4.17.1",
"node-fetch": "^2.6.0",
"redis": "^3.0.2",
"redredisearch": "^0.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.