消息队列(.Net)


using Abp.Notifications;
using Abp.Runtime.Caching;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BM.ERP.Notifications
{
    public class CacheNotificationManager : ERPDomainServiceBase
    {
        private readonly ICacheManager _cacheManager;

        private ITypedCache<long, List<CacheNotificationClient>> _OnlientClientCache => _cacheManager.GetCache<long, List<CacheNotificationClient>>("NotificationStore.OnlineClients");

        private ITypedCache<string, List<UserNotification>> _NotificationCache => _cacheManager.GetCache<string, List<UserNotification>>("NotificationStore.Message");

        private static ConcurrentDictionary<long, object> lockDic = new ConcurrentDictionary<long, object>();

        public CacheNotificationManager(ICacheManager cacheManager)
        {
            _cacheManager = cacheManager;
        }

        /// <summary>
        /// 插入新消息到缓存队列
        /// </summary>
        /// <param name="userNotification"></param>
        public void PushCacheNotification(UserNotification userNotification)
        {
            long userId = userNotification.UserId;
            
            List<CacheNotificationClient> clients = GetAllClientByUserId(userId);

            Logger.Debug($"PushCacheNotification {userNotification.Notification.NotificationName} to {userNotification.UserId},clients={clients?.Count}");

            if (clients != null && clients.Count > 0)
            {

                foreach (var client in clients)
                {
                    string key = $"{userId}@{client.ClientId}";

                    lock (lockDic.GetOrAdd(client.UserId, new object()))
                    {
                        List<UserNotification> cachedUserNotifications = _NotificationCache.GetOrDefault(key);
                        if (cachedUserNotifications == null)
                        {
                            cachedUserNotifications = new List<UserNotification>();
                        }

                        List<UserNotification> newUserNotifications = cachedUserNotifications.Where(p => p.Notification.CreationTime > DateTime.Now.AddMinutes(-30)).ToList();

                        newUserNotifications.Add(userNotification);

                        _NotificationCache.Set(key, newUserNotifications);
                    }
                }
            }
        }

        /// <summary>
        /// 拉取消息,并清空消息队列
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="clientId"></param>
        /// <returns></returns>
        public List<UserNotification> PopCacheNotification(long userId, string clientId)
        {
            lock (lockDic.GetOrAdd(userId, new object()))
            {
                string key = $"{userId}@{clientId}";

                List<UserNotification> cachedUserNotifications = _NotificationCache.GetOrDefault(key);
                _NotificationCache.Remove(key);
                return cachedUserNotifications;
            }
        }

        /// <summary>
        /// 获取所有客户端
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<CacheNotificationClient> GetAllClientByUserId(long userId)
        {
            return _OnlientClientCache.GetOrDefault(userId);
        }

        /// <summary>
        /// 注册客户端
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="clientId"></param>
        public void RegistClient(long userId, string clientId)
        {
            lock (lockDic.GetOrAdd(userId, new object()))
            {
                List<CacheNotificationClient> list = GetAllClientByUserId(userId);

                if (list == null)
                {
                    list = new List<CacheNotificationClient>();
                }

                CacheNotificationClient client = null;

                client = list.FirstOrDefault(p => p.ClientId == clientId);

                if (client == null)
                {
                    client = new CacheNotificationClient()
                    {
                        ClientId = clientId,
                        UserId = userId,
                        LastPingTime = DateTime.Now
                    };
                    list.Add(client);
                    _OnlientClientCache.Set(userId, list);
                }
                else
                {
                    client.LastPingTime = DateTime.Now;
                }
            }
        }

    }

    /// <summary>
    /// 客户端
    /// </summary>
    public class CacheNotificationClient
    {
        /// <summary>
        /// 客户端id 唯一
        /// </summary>
        public string ClientId { get; set; }

        /// <summary>
        /// 用户id
        /// </summary>
        public long UserId { get; set; }

        /// <summary>
        /// 最后ping时间
        /// </summary>
        public DateTime LastPingTime { get; set; }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值