当前位置:网站首页>asp.net 使用redis缓存(二)
asp.net 使用redis缓存(二)
2022-07-26 09:23:00 【跑马的汉子睡眠不足】
今天我们介绍3台Redis服务器(哨兵模式,一主两从三哨兵)情况下,asp.net项目中如何运用redis做缓存,话不多说,直接上代码。
关于redis哨兵模式搭建,可翻阅我之前的博文Windows下Redis哨兵模式搭建了解一下。
csRedisHelper.cs
using CSRedis;
using System;
public class csRedisHelper
{
private static readonly object LockObj = new object();
private static csRedisHelper _instance;
public static csRedisHelper Instance
{
get
{
if (_instance == null)
{
lock (LockObj)
{
if (_instance == null)
_instance = new csRedisHelper();
}
}
return _instance;
}
}
private csRedisHelper()
{
try
{
var sentinels = "127.0.0.1:26379,127.0.0.1:26479,127.0.0.1:26579";//Redis哨兵
var connString = "mymaster, password=123456, poolsize=200,preheat=false";//redis的连接字符串
if (string.IsNullOrWhiteSpace(sentinels) || string.IsNullOrWhiteSpace(connString))
throw new ArgumentNullException("配置文件中未找到RedisServer的有效配置");
var arraySentinels = SplitString(sentinels);
if (arraySentinels == null || arraySentinels.Length <= 0)
throw new ArgumentNullException("配置文件中未找到RedisServer的有效配置");
var _csRedis = new CSRedisClient(connString, arraySentinels);
RedisHelper.Initialization(_csRedis);
}
catch (Exception ex)
{
//Logger.GetLogger("csRedisHelper").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 RedisHelper.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
{
return RedisHelper.Set(key, t, expireSeconds);
}
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 RedisHelper.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)
{
RedisHelper.Del(key);
}
/// <summary>
/// 从缓存中移除指定键的缓存值
/// </summary>
/// <param name="pattern"></param>
public void RemoveByPattern(string pattern)
{
var keys = RedisHelper.Keys(pattern + "*");
if (keys != null && keys.Length > 0)
RedisHelper.Del(keys);
}
private string[] SplitString(string str)
{
return str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
}
csRedisHelper.Instance.Insert("UserName", "张三");//写入
var userName = csRedisHelper.Instance.Get<string>("UserName");//读取
var test = new Test
{
UserName = "张三",
Email = "[email protected]"
};
csRedisHelper.Instance.Insert("Test", test);
var getTest = csRedisHelper.Instance.Get<Test>("Test");
var testList = new List<Test>
{
new Test
{
UserName="张三",
Email="[email protected]"
}, new Test
{
UserName="李四",
Email="[email protected]"
}
};
csRedisHelper.Instance.Insert("TestList", testList);
var getTestList = csRedisHelper.Instance.Get<List<Test>>("TestList");
CSRedis.dll和SafeObjectPool.dll 下载链接:https://pan.baidu.com/s/1udPoQZ-LgSrTkhOZHE4yJw
提取码:56yq
边栏推荐
- mysql5.7.25主从复制(单向)
- Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
- 选择器的使用
- arc-gis的基本使用2
- Qt | 关于如何使用事件过滤器 eventFilter
- Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
- 省政府召开全省高温天气安全防范工作电视电话会议
- 什么是异步操作
- 解决“NOTE: One or more layouts are missing the layout_width or layout_height attributes.”
- Bloom filter
猜你喜欢

antUI中a-modal 拖拽功能制作

Selection and practice of distributed tracking system

JVM command induction

What are CSDN spaces represented by

异常处理机制二

js在控制台输出菱形

2022 mobile crane driver test question simulation test question bank simulation test platform operation

Cat installation and use

Basic use of Arc GIS 2

Personality test system V1.0
随机推荐
Paper notes: knowledge map kgat (unfinished temporary storage)
服务器内存故障预测居然可以这样做!
【Mysql】redo log,undo log 和binlog详解(四)
异常处理机制二
Use of off heap memory
Force button list question
[online problem] timeout waiting for connection from pool problem troubleshooting
arcgis的基本使用1
The Child and Binary Tree-多项式开根求逆
760. 字符串长度
TabbarController的封装
Windows backs up the database locally by command
Vertical search
大二上第三周学习笔记
Cat installation and use
语音聊天app源码——钠斯直播系统源码
2022 mobile crane driver test question simulation test question bank simulation test platform operation
docker配置mysql集群
VectorTileLayer更换style
Windows通过命令备份数据库到本地