123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using FreeRedis;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace WMS.BZServices
- {
- public interface IRedisService
- {
- int DistributeLock(string lockKey, int timeoutSeconds = 30, bool autoDelay = false);
- string Get(string key = "key");
- T Get<T>(string key = "key");
- long Publish(string channel, string message);
- bool Set(string key, string value, TimeSpan timeSpan);
- bool Set<T>(string key, T value, TimeSpan timeSpan);
- void Del(string key);
- }
- public class RedisService : IRedisService
- {
- private readonly RedisClient _redisClient;
- private static int _tempInt = 0;
- /// <summary>
- ///
- /// </summary>
- /// <param name="logger"></param>
- /// <param name="redisIdleBus"></param>
- public RedisService( string redisconnect)
- {
- _redisClient = new RedisClient(redisconnect);
- _redisClient.Serialize = obj => JsonConvert.SerializeObject(obj); ;// JsonConvert.SerializeObject(obj);
- _redisClient.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);// JsonConvert.DeserializeObject(json, type);
- }
- /// <summary>
- /// Redis插入
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <param name="timeSpan"></param>
- /// <returns></returns>
- public bool Set(string key, string value, TimeSpan timeSpan)
- {
- _redisClient.Set(key, value, timeSpan);
-
- return true;
- }
- public bool Set<T>(string key, T value, TimeSpan timeSpan)
- {
- _redisClient.Set<T>(key, value, timeSpan);
-
- return true;
- }
- /// <summary>
- /// 通过key或者Redis值
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string Get(string key)
- {
- //可随时改用原生对象操作redis
- var value = _redisClient.Get<string>(key);
-
- return value;
- }
- public T Get<T>(string key)
- {
- //可随时改用原生对象操作redis
- var value = _redisClient.Get<T>(key);
- return value;
- }
- public void Del(string key)
- {
- _redisClient.Del(key);
- }
- /// <summary>
- /// 消息发布
- /// </summary>
- /// <param name="channel"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- public long Publish(string channel, string message)
- {
- var value = _redisClient.Publish(channel, message);
-
- return value;
- }
- /// <summary>
- /// 分布式锁
- /// </summary>
- /// <param name="lockKey">指定锁的key</param>
- /// <param name="timeoutSeconds">超时秒数,超过时间自动释放锁</param>
- /// <param name="autoDelay">是否需要自动延时,true:自动延长过期时间false:不延长过期时间,以设置的过期时间为准</param>
- /// <returns></returns>
- public int DistributeLock(string lockKey, int timeoutSeconds = 30, bool autoDelay = false)
- {
- //通过别名为1的redis库进行分布式锁
- using (_redisClient.Lock(lockKey, timeoutSeconds, autoDelay))
- {
- //被锁住的逻辑
-
- return _tempInt;
- }
- }
- }
- }
|