As a result of National Day period wife adult suddenly sees in the car front license plate soul to ask to say xiang F is where 😱 I which know where, I can only answer a sentence: I also don’t know 😂. In order to avoid the occurrence of similar soul to ask, I wrote a micro channel small program to facilitate query, wit 😜 data are on the Internet to climb, if interested in understanding how to climb can view the previous article [NodeJS climb news]; After the data is removed, it is found that you can add the function of querying basic information of the location, well, thanks to Baidu Baike. Considering that the data is crawled in real time, I thought that the location information may be constant for thousands of years, so I introduced Redis cache to process.
The local installation
Windows 10 is used locally.
- Download the corresponding program, decompress to the local
- Go to the directory and run the command
./redis-server.exe redis.windows.conf
The specific Redis installation can be viewedNovice tutorial
Program dependency package
Download the Redis installation package
yarn add redis
Copy the code
Open the service
Reference the corresponding package and connect to the service
const redis = require('redis');
const redisClient = redis.createClient({
host: '127.0.0.1'./ / the default host
port: '6379' // Default port
});
Copy the code
Data manipulation
You can use promisify to wrap operations into asynchronous functions
const redis = require('redis');
const { promisify } = require("util");
const config = require('.. /config');
const redisClient = redis.createClient(config.REDIS_PORT);
redisClient.on("error".function (err) {
console.log("Error " + err);
});
function Client() {
this.set = promisify(redisClient.set).bind(redisClient);
this.get = promisify(redisClient.get).bind(redisClient);
return this;
}
const client = new Client();
module.exports = client
Copy the code
Refer to operations when you need to manipulate data
const redisStore = require('.. /utils/redis');
const getBaike = async (site) => {
console.log(`site: ${site}`);
if(! site) {return null; }...const data = {
introduce,
basicInfo
};
await redisStore.set(site, JSON.stringify(data));
return data;
};
module.exports = async (ctx, next) => {
await next();
const { site } = ctx.query;
const cacheData = await redisStore.get(site);
const baike = cacheData ? JSON.parse(cacheData) : await getBaike(site);
ctx.type = 'json';
ctx.body = {
baike
}
}
Copy the code
A simple Redis service operation is done.
Pay attention to
Since the production environment is Linux, it is impossible to start the Redis service in this front-end startup mode, so we need to change itredis.conf
The configuration,daemonize yes
The backend mode is started.Start the service again
cd /opt/redis
./bin/redis-server ./redis.conf
Copy the code
Connect the redis
/opt/redis/bin/redis-cli
Copy the code
The last
Use their knowledge to solve some problems in life, feel good, we can experience or have good ideas, we communicate together.
Reference:
- Redis installation and deployment under Linux
- Redis tutorial