我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 认识ESM
  • chrome-talend插件,类似postman
  • sequelize 使用及技巧
  • UML工具Power Designer建表
  • sequelize相关操作文档
  • 解决抖音获取签名及并发的问题
  • 记一次解决抖音分享页混淆字体,字体图标转UID解决方案
  • 获取抖音用户作品列表信息
  • 获取抖音用户作品列表信息-进阶
  • 获取抖音用户作品列表信息-进阶3
  • 如何根据抖音号获取用户信息
  • 获取用户最新视频
  • 模块化-import和require的区别
  • eslint规范
  • js容错处理
  • js-数组分组,执行promise
  • reduce使用遇到的问题
  • 正则匹配html的元素内容
  • taro 小程序 弹窗层禁止底部滚动
  • 公众号签名问题
  • CentOS7中MariaDB重置密码
  • nginx多域名配置
  • node访问接口,得到乱码的结果,原因-Accept-Encoding
  • node写文件到json中
  • node抓取html内容
  • Node.js使用ES6语法
  • express 使用cors中间件解决跨域
  • node + express + session + redis 进行持久化缓存
  • node中读取文件夹,获取文件名称
  • pm2常用命令
  • 使用pm2管理后台node服务
  • typescript puppeteer支持window及document属性
  • node读取json文件
  • node中使用redis缓存
    • redis安装
    • 连接redis
    • redis 设置 取值
      • 设置值
      • 取值
    • 总结
  • node + github的webhook完成自动部署
  • vuepress-blog的性能优化-CDN
  • CENTOS7下安装REDIS
  • promise then和catch的学习和使用
  • promise在循环中的串行并行用法
  • puppeteer常用知识
  • centos部署安装puppeteer
  • python的学习和使用
  • Taro+TypeScript - Mobx实践
  • 爬虫系列 --- 反爬机制和破解方法汇总
  • 安全-html转码
  • taro中使用animation动画
  • charles 使用
  • Mac下VSCode设置iTerm2终端样式
  • centos一步步完成站点部署
  • 云闪付做地铁的思路
  • 准备技能
  • 备案pc项目介绍
  • 备案小程序项目介绍
  • 小程序二维码扫码功能
  • 小程序域名组件开发
  • 小程序添加水印
  • 规则引擎优化
  • 记一次hooks代替redux的经历
  • 通过nodejs+koa+stream进行服务端图片代理
  • nodeJs接入log4j日志
  • nodejs+typescript项目中添加全局global属性
  • create-react-app 安装 bizcharts 项目崩溃
  • 使用MutationObserver监控dom的变化
  • 服务器重启后启动相关服务
  • moment国际化的问题
  • 项目经验
fangdown
2019-12-12
目录

node中使用redis缓存

# redis安装

CentOS 7 安装 Redis4.0.2 (opens new window)

# 连接redis

import * as IORedis from 'ioredis';
import { logger } from 'fd-framework';
const log = logger.get('framework');

const config = {
  alias: 'app-redis',
  port: 6379, // Redis port
  host: '148.70.216.46', // Redis host
  family: 4, // 4 (IPv4) or 6 (IPv6)
  password: 'fang674123',
  db: 0,
};
const init = () => {
  const client = new IORedis(config);
  client.on('connect', () => {
    const msg = 'Connected';
    log.info(`[${config.alias}]: ${msg}`);
  });

  client.on('ready', () => {
    const msg = 'Ready';
    log.info(`[${config.alias}]: ${msg}`);
  });

  client.on('reconnecting', () => {
    const msg = 'Reconnecting';
    log.info(`[${config.alias}]: ${msg}`);
  });

  client.on('end', () => {
    const msg = 'Closed';
    log.info(`[${config.alias}]: ${msg}`);
  });

  client.on('error', (err: Error) => {
    log.error(`[${config.alias}]: ${err.message}`);
  });
};
export default { init };

# redis 设置 取值

# 设置值

/**
 * 设置redis
 * @param session
 * @param key
 * @param value
 */
export async function setRedisData(
  key: string,
  value: Record<string, any> | string,
  cacheTime?: number
): Promise<any> {
  const redisClient = authRedis ? redis.get(authRedis) : null;
  if (!redisClient || !redisClient.isReady) {
    const msg = `redis ${redisClient ? redisClient.name : ''} connect failed`;
    throw new ServiceError(
      SERVICE_CODE.REDIS,
      ERROR_CODE.INTERNAL_SERVER_ERROR,
      msg
    );
  }
  const cacheKey = `${key}`;
  const cacheData = JSON.stringify(value);
  log.info('cacheData', cacheData);
  await redisClient.set(
    cacheKey,
    cacheData,
    cacheTime ? cacheTime : CACHE_TIME
  );
}

# 取值

/**
 * redis取值
 * @param session
 * @param key
 * @param value
 */
export async function getRedisData(key: string): Promise<any> {
  const redisClient = authRedis ? redis.get(authRedis) : null;
  if (!redisClient || !redisClient.isReady) {
    const msg = `redis ${redisClient ? redisClient.name : ''} connect failed`;
    throw new ServiceError(
      SERVICE_CODE.REDIS,
      ERROR_CODE.INTERNAL_SERVER_ERROR,
      msg
    );
  }
  const cacheKey = `${key}`;
  log.info('cacheKey', cacheKey);
  try {
    const cacheData = await redisClient.get(cacheKey);
    return cacheData ? JSON.parse(cacheData) : '';
  } catch (e) {
    log.error('get Redis data failed:', e);
    return '';
  }
}

# 总结

redis可简单缓存一些数据,如微信token,session等

#redis
上次更新: 2021/12/19, 18:05:42
node读取json文件
node + github的webhook完成自动部署

← node读取json文件 node + github的webhook完成自动部署→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式