.NetCore 操作Redis 实现增、删、改、查(模糊查询)、分布式锁 。
需要安装RedisHelper 包
using Microsoft.Extensions.Configuration; using StackExchange.Redis; using System; using System.Threading; namespace Redis { public class RedisHelper { #region Fileds private static string _redisConnection; //= "120.79.77.91:6379,defaultDatabase=0,password=mixdo2018"; private static int _db = 0; private static ConnectionMultiplexer connection; #endregion #region Constructors public RedisHelper(IConfiguration configuration) { _redisConnection = configuration["RedisConfigHost.Connection"]?.ToString() ?? ""; } public static ConnectionMultiplexer CacheConnection { get { try { if (connection == null || !connection.IsConnected) { connection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(_redisConnection)).Value; } } catch (Exception ex) { return null; } return connection; } } #endregion #region Methons /// <summary> /// 缓存当前数据库 /// </summary> public static IDatabase CacheRedis => CacheConnection.GetDatabase(_db); /// <summary> /// 新增单条值 /// </summary> /// <param name="values"></param> /// <returns></returns> public static bool StringSet(string key, string values) { if (string.IsNullOrEmpty(key) && string.IsNullOrEmpty(values)) throw new AggregateException("values or is null"); return CacheRedis.StringSet(key, values); } /// <summary> /// 查询单个key值 /// </summary> /// <param name="key"></param> /// <returns></returns> public static RedisValue GetStringKey(string key) { return CacheRedis.StringGet(key); } /// <summary> /// 判断key是否存储 /// </summary> /// <param name="key">redis key</param> /// <returns></returns> public bool KeyExists(string key) { return CacheRedis.KeyExists(key); } /// <summary> /// 删除单个key /// </summary> /// <param name="key">redis key</param> /// <returns>是否删除成功</returns> public bool KeyDelete(string key) { return CacheRedis.KeyDelete(key); } /// <summary> /// redis 枷锁 /// </summary> /// <param name="key"></param> /// <param name="expireTimeSeconds">到期时间秒</param> /// <exception cref="Exception"></exception> #region 分布式锁 public static bool LockByRedis(string key, string values) { try { //expireTimeSeconds = expireTimeSeconds > 20 ? 10 : expireTimeSeconds; //var data = TimeSpan.FromSeconds(expireTimeSeconds); //var token = Environment.MachineName; //bool lockflag = CacheRedis.LockTake(key, Thread.CurrentThread.ManagedThreadId, TimeSpan.FromSeconds(expireTimeSeconds)); bool lockflag = CacheRedis.LockTake(key, values, TimeSpan.FromSeconds(60));//TimeSpan.MaxValue if (!lockflag) { return false; } return true; } catch (Exception ex) { Console.WriteLine($"Redis加锁异常:原因{ex.Message}"); throw new Exception($"Redis加锁异常:原因{ex.Message}"); } } /// <summary> /// 解锁 /// </summary> /// <param name="key"></param> /// <returns></returns> /// <exception cref="Exception"></exception> public static bool UnLockByRedis(string key, string valuse) { try { //Thread.CurrentThread.ManagedThreadId //Environment.MachineName return CacheRedis.LockRelease(key, valuse); } catch (Exception ex) { Console.WriteLine($"Redis解锁异常:原因{ex.Message}"); throw new Exception($"Redis解锁异常:原因{ex.Message}"); } } #endregion #endregion #region Utilities #endregion } }
使用redis进行模糊查询 方式如下
浙公网安备 33010602011771号