队列解决redis插入并发问题

csharp

浏览数:253

2019-1-8

using Newtonsoft.Json;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace QueueDemo
{
    public class QueueCommon
    {
        public string Key { get; set; }
        public object Value { get; set; }
        public DateTime? ExpreTime { get; set; }
    }
    public class RedisQueueHelper
    {
        private static readonly RedisQueueHelper _instance = new RedisQueueHelper();
        private RedisQueueHelper()
        {
            redisMgr = new PooledRedisClientManager("127.0.0.1");
        }
        public static RedisQueueHelper GetInstance()
        {
            if (_instance != null)
            {
                return _instance;
            }
            return null;
        }
        private Queue<QueueCommon> RedisQueue = new Queue<QueueCommon>();
        public PooledRedisClientManager redisMgr;
        public void AddQueue<T>(string key, T data)
        {
            
            QueueCommon model = new QueueCommon();
            model.Key = key;
            model.Value = data;
            RedisQueue.Enqueue(model);
        }
        public void AddQueue<T>(string key, T data, DateTime expreTime)
        {
            QueueCommon model = new QueueCommon();
            model.Key = key;
            model.Value = data;
            model.ExpreTime = expreTime;
            RedisQueue.Enqueue(model);
        }
        public void ThreadStartQueue()
        {
            Thread thread = new Thread(WriteRedis);
            thread.IsBackground = true;
            thread.Start();
            ThreadState = thread.ThreadState;
        }
        public ThreadState ThreadState { get; set; }
        private void WriteRedis()
        {
            try
            {
                if (RedisQueue.Count < 1)
                {
                    Thread.Sleep(3000);
                }
                using (IRedisClient client = redisMgr.GetClient())
                {
                    while (RedisQueue.Count > 0)
                    {
                        QueueCommon model = RedisQueue.Dequeue();
                        if (model.ExpreTime != null)
                        {
                            client.Set(model.Key, model.Value, (DateTime)model.ExpreTime);
                            continue;
                        }
                        client.Set(model.Key, model.Value);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public T Get<T>(string key)where T: class
        {
            using (IRedisClient client = redisMgr.GetClient())
            {
                return client.Get<T>(key);
            }
        }
        public void Remove(string key)
        {
            using (IRedisClient client = redisMgr.GetClient())
            {
                client.Remove(key);
            }
        }
    }
}