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

NTT (fast number theory transformation) polynomial inverse 1500 word analysis

2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation

JS output diamond on the console

Basic use of Arc GIS 2
![[shutter -- layout] detailed explanation of the use of align, center and padding](/img/01/c588f75313580063cf32cc01677600.jpg)
[shutter -- layout] detailed explanation of the use of align, center and padding

js中树与数组的相互转化(树的子节点若为空隐藏children字段)

Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain

【Flutter -- 布局】Align、Center、Padding 使用详解

Li Mu D2L (IV) -- softmax regression
![[MySQL] detailed explanation of MySQL lock (III)](/img/3c/c6b5aa5693e6e7b5730a286d988c82.png)
[MySQL] detailed explanation of MySQL lock (III)
随机推荐
PHP 之 Apple生成和验证令牌
[shutter -- layout] detailed explanation of the use of align, center and padding
Li Mu D2L (V) -- multilayer perceptron
C# Serialport的发送和接收
TableviewCell高度自适应
Tornado multi process service
大二上第二周学习笔记
JS closure: binding of functions to their lexical environment
Original root and NTT 5000 word explanation
[MySQL] detailed explanation of MySQL lock (III)
“No input file specified “问题的处理
cocoapods的安装和使用
Vertical search
MySQL 强化知识点
[MySQL] how to execute an SQL statement (2)
"No input file specified" problem handling
ZXing简化版,转载
2022 mobile crane driver test question simulation test question bank simulation test platform operation
Processing of inconsistent week values obtained by PHP and MySQL
(2006, MySQL server has gone away) problem handling