当前位置:网站首页>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
边栏推荐
猜你喜欢

Nuxt - Project packaging deployment and online to server process (SSR server rendering)

Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing

NTT(快速数论变换)多项式求逆 一千五百字解析

【Mysql】认识Mysql重要架构(一)

Voice chat app source code - Nath live broadcast system source code

(2006, MySQL server has gone away) problem handling

csdn空格用什么表示

The Child and Binary Tree-多项式开根求逆

Object 的Wait Notify NotifyAll 源码解析

jvm命令归纳
随机推荐
C# Serialport的发送和接收
大二上第三周学习笔记
Mysql事务
Study notes of canal
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
Your login IP is not within the login mask configured by the administrator
Li Mu D2L (VI) -- model selection
语音聊天app源码——钠斯直播系统源码
什么是异步操作
Sliding window, double pointer, monotone queue, monotone stack
Android implements the caching mechanism and caches multiple data types
760. 字符串长度
Elastic APM installation and use
Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
Canal 的学习笔记
微信小程序学习笔记2
Basic use of ArcGIS 4
暑假第四周
神经网络与深度学习-6- 支持向量机1 -PyTorch
安卓 实现缓存机制,多种数据类型缓存