1. 程式人生 > 其它 >C# Redis訊息佇列例子2

C# Redis訊息佇列例子2

  • 2 建立類

建立介面IRedisCache.cs和RedisCache.cs

using H.Emos.Common.Helper;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
 
namespace H.Emos.Common.Redis
{
    public class RedisCache : IRedisCache
    {
        /// <summary>
/// 連線字串 /// </summary> private readonly string ConnectionString; /// <summary> /// redis 連線物件 /// </summary> private volatile IConnectionMultiplexer redisConnection; /// <summary> ////// </summary> private
readonly object redisConnectLock = new object(); public RedisCache() { this.ConnectionString = "127.0.0.1:6379,allowadmin=true,password=123456"; this.redisConnection = getConnection(); } private IConnectionMultiplexer getConnection() {
if(this.redisConnection!=null&&this.redisConnection.IsConnected) { return this.redisConnection; } lock(redisConnectLock) { if(this.redisConnection != null) { this.redisConnection.Dispose(); } try { this.redisConnection = ConnectionMultiplexer.Connect(this.ConnectionString); } catch (Exception) { throw new Exception("redis 服務未啟動") ; } return this.redisConnection; } } public void Clear() { foreach (var endPoint in this.getConnection().GetEndPoints()) { var server = this.getConnection().GetServer(endPoint); foreach (var key in server.Keys()) { redisConnection.GetDatabase().KeyDelete(key); } } } public long publish<T>(string topic, T message) { var subscriber= redisConnection.GetSubscriber(); return subscriber.Publish(topic,SerializeHelper.SerializeString(message)); } public string subscriber(string topic) { var msg = string.Empty; var subscriber = redisConnection.GetSubscriber(); subscriber.Subscribe(topic,(channel,message)=> { msg=message; }); return msg; } public string stringGet(string key) { return redisConnection.GetDatabase().StringGet(key); } public bool stringSet(string key, string keyValue,TimeSpan time) { return redisConnection.GetDatabase().StringSet(key,keyValue,time); } public bool stringSet<T>(string key, T keyValue,TimeSpan time) { return redisConnection.GetDatabase().StringSet(key, SerializeHelper.Serialize(keyValue),time); } public async Task<bool> taskStringSet(string key, string keyValue) { return await redisConnection.GetDatabase().StringSetAsync(key, keyValue); } } }
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
 
namespace H.Emos.Common.Redis
{
     public interface IRedisCache
    {
        bool stringSet(string key, string keyValue,TimeSpan time);
        string stringGet(string key);
        bool stringSet<T>(string key, T keyVale,TimeSpan time);
 
        Task<bool> taskStringSet(string key, string keyValue);
        long publish<T>(string topic,T message);
        string subscriber(string topic);
    }
}
————————————————
版權宣告:本文為CSDN博主「Wonderful1025」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/Wonderful1025/article/details/106475960/

在WeatherForestcastController中建構函式注入,然後測試

[HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
         //   _redisCache.stringSet("test01","test01",TimeSpan.FromHours(5));
 
            WeatherForecast weatherForecast = new WeatherForecast();
            weatherForecast.Date = DateTime.Now;
            weatherForecast.TemperatureC = rng.Next(-20, 55);
            weatherForecast.Summary = Summaries[rng.Next(2)];
            var value=_redisCache.publish("redisChat",weatherForecast);
          
            var tt= Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
 
            return tt;
 
        }
————————————————
版權宣告:本文為CSDN博主「Wonderful1025」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/Wonderful1025/article/details/106475960/
人各有命,上天註定,有人天生為王,有人落草為寇。腳下的路,如果不是你自己的選擇,那麼旅程的終點在哪,也沒人知道。你會走到哪,會遇到誰,都不一定。