当前位置:网站首页>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
边栏推荐
- jvm命令归纳
- QT | about how to use EventFilter
- a-table中的rowSelection清空问题
- PMM(Percona Monitoring and Management )安装记录
- Study notes of canal
- 性格测试系统v1.0
- 暑假第四周
- Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
- js中树与数组的相互转化(树的子节点若为空隐藏children字段)
- "Could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32"
猜你喜欢
服务器内存故障预测居然可以这样做!
Original root and NTT 5000 word explanation
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
mysql5.7.25主从复制(单向)
2022 chemical automation control instrument operation certificate test question simulation test platform operation
Object 的Wait Notify NotifyAll 源码解析
[MySQL] detailed explanation of redo log, undo log and binlog (4)
Server memory failure prediction can actually do this!
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
Zipkin installation and use
随机推荐
PHP和MySQL获取week值不一致的处理
MySql5.7.25源码安装记录
Original root and NTT 5000 word explanation
VS2019配置opencv
暑假末尾学习笔记
Local cache
JVM command induction
Pat grade a A1034 head of a gang
【线上死锁分析】由index_merge引发的死锁事件
Study notes of dataX
Zxing simplified version, reprinted
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
Conditions for JVM to trigger minor GC
2022 mobile crane driver test question simulation test question bank simulation test platform operation
Object type collections are de duplicated according to the value of an attribute
arcgis的基本使用1
What are CSDN spaces represented by
小程序纪录
Basic use of ArcGIS 4
el-table的formatter属性的用法