当前位置:网站首页>asp.net 使用redis缓存
asp.net 使用redis缓存
2022-07-26 09:23:00 【跑马的汉子睡眠不足】
一、Redis下载安装
1. 下载地址:https://github.com/tporadowski/redis/releases
按需选择安装,我这里下载的Redis-x64-5.0.10.zip
2. 安装配置redis
1)解压Redis-x64-5.0.10.zip,将文件夹重新命名为 redis
2)修改redis.windows.conf 文件内容如需:
3)打开一个cmd窗口,使用cd命令切换目录到 E:\redis\6879,运行如下命令
redis-server.exe --service-install redis.windows.conf --service-name redis6879 --port 6879
4)启动Redis服务
redis-server.exe --service-start --service-name Redis6380
5)连接登录
redis-cli.exe -h 127.0.0.1 -p 6879 -a 123456
如果习惯命令操作redis,可以安装一个 redis可视化工具 RedisDesktopManager。
redis desktop manager是一款功能强大的redis数据库管理软件,可以帮助用户轻松快速的查看与操控整个数据库。redis desktop manager不仅拥有十分简洁直观的操作界面,而且所有功能信息一目了然,是广大用户必备的数据库管理神器。
redis desktop manager具有操作简单、方便快捷、功能完善、性能稳定等优点,支持用户采用可视化操作界面对数据库进行各方面工作,不管是新手用户还是专业的开发人员,该软件都是你管理数据库的最佳帮手。
redis安装好了,程序里面怎么使用它来做缓存呢,直接上代码
二、使用redis做缓存服务器
RedisHelper.cs
using System;
using ServiceStack.Redis;
public class RedisHelper
{
private static readonly object LockObj = new object();
private static RedisHelper _instance;
private static RedisClient _redisClient;
public static RedisHelper Instance
{
get
{
if (_instance == null)
{
lock (LockObj)
{
if (_instance == null)
_instance = new RedisHelper();
}
}
return _instance;
}
}
private RedisHelper()
{
try
{
var host ="127.0.0.1";
var port =6879;
var pwd ="123456";
if (string.IsNullOrWhiteSpace(host) || port <= 0)
throw new ArgumentNullException("配置文件中未找到RedisServer的有效配置");
_redisClient = new RedisClient(host, port);
if (!string.IsNullOrWhiteSpace(pwd))
{
_redisClient.Password = pwd;
//_redisClient.Db= 0;
}
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis出现异常,\r\n 异常消息:{0}", ex.Message);
}
}
/// <summary>
/// 写入
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <returns></returns>
public bool Insert<T>(string key, T t)
{
try
{
return _redisClient.Set(key, t);
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis出现异常,\r\n 异常消息:{0}", ex.Message);
}
return false;
}
/// <summary>
/// 写入
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="expireSeconds">过期时间 秒</param>
/// <returns></returns>
public bool Insert<T>(string key, T t, int expireSeconds)
{
try
{
var r = _redisClient.Set(key, t);
if (r)
{
_redisClient.Expire(key, expireSeconds); //设置指定Key的过期时间
}
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis出现异常,\r\n 异常消息:{0}", ex.Message);
}
return false;
}
/// <summary>
/// 读取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T Get<T>(string key)
{
try
{
return _redisClient.Get<T>(key);
}
catch (Exception ex)
{
//Logger.GetLogger(this.GetType()).ErrorFormat("Redis出现异常,\r\n 异常消息:{0}", ex.Message);
}
return default(T);
}
/// <summary>
/// 从缓存中移除指定键的缓存值
/// </summary>
/// <param name="key"></param>
public void Remove(string key)
{
_redisClient.Remove(key);
}
/// <summary>
/// 从缓存中移除指定键的缓存值
/// </summary>
/// <param name="pattern"></param>
public void RemoveByPattern(string pattern)
{
var keys = _redisClient.SearchKeys(pattern + "*");
if (keys != null && keys.Count > 0)
_redisClient.RemoveAll(keys);
}
}
RedisHelper.Instance.Insert("UserName", "张三");//写入
var userName = RedisHelper.Instance.Get<string>("UserName");//读取
var test = new Test
{
UserName = "张三",
Email = "[email protected]"
};
RedisHelper.Instance.Insert("Test", test);
var getTest = RedisHelper.Instance.Get<Test>("Test");
var testList = new List<Test>
{
new Test
{
UserName="张三",
Email="[email protected]"
}, new Test
{
UserName="李四",
Email="[email protected]"
}
};
RedisHelper.Instance.Insert("TestList", testList);
var getTestList = RedisHelper.Instance.Get<List<Test>>("TestList");
public class Test
{
public string UserName { set; get; }
public string Email { set; get; }
}
好了,Redis简单的缓存使用就到这了,要注意的是ServiceStack有一个巨大的坑, 从4.0版本后开始商业化了,限制每小时默认访问次数是6000次,这个在一个稍微大一点的项目里, 没几分钟就够了, 除非购买license或者降低版本使用。
ServiceStack.dll 1.0版本下载链接:https://pan.baidu.com/s/1dLc7vMc_Pzh_VExyyKHgMg
提取码:b339
边栏推荐
- el-table实现增加/删除行,某参数跟着变
- jvm命令归纳
- 服务器内存故障预测居然可以这样做!
- 【线上死锁分析】由index_merge引发的死锁事件
- 吴恩达机器学习之线性回归
- PMM(Percona Monitoring and Management )安装记录
- Order based evaluation index (especially for recommendation system and multi label learning)
- Server memory failure prediction can actually do this!
- JS - DataTables control on the number of displays per page
- Cat installation and use
猜你喜欢
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
2022 mobile crane driver test question simulation test question bank simulation test platform operation
JVM command induction
volatile 靠的是MESI协议解决可见性问题?(上)
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
2022 Shanghai safety officer C certificate examination questions and mock examination
MySQL transaction
Li Mu D2L (IV) -- softmax regression
Go intelligent robot alpha dog, alpha dog robot go
(2006, MySQL server has gone away) problem handling
随机推荐
OnTap 9 file system limitations
Paper notes: knowledge map kgat (unfinished temporary storage)
自定义密码输入框,无圆角
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
[shutter -- layout] detailed explanation of the use of align, center and padding
大二上第三周学习笔记
Windows通过命令备份数据库到本地
【Flutter -- 布局】Align、Center、Padding 使用详解
C# Serialport的发送和接收
暑假第四周
微信小程序学习笔记1
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
微信小程序学习笔记2
神经网络与深度学习-6- 支持向量机1 -PyTorch
吴恩达机器学习之线性回归
【Mysql】一条SQL语句是怎么执行的(二)
语音聊天app源码——钠斯直播系统源码
cocoapods的安装和使用
Canal 的学习笔记
TableviewCell高度自适应