|
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TelpoPush.Models.CacheTemplates;
- using TelpoPush.Models.Config;
-
- namespace TelpoPush.Service.Cache
- {
- public class RedisUtil
- {
- private const string CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG = "TELPO#MANUFACTOR_CONFG_HASH";
- private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS = "TELPO#GPSDEVICE_PUSH_SITTINGS_HASH";
-
- private readonly ILogger<RedisUtil> _logger;
- private readonly ServiceConfig _configService;
- private readonly SqlMapper _sqlMapper;
- public RedisUtil(ILogger<RedisUtil> logger,IOptions<RedisConfig> optConfigRedis, IOptions<ServiceConfig> configService, SqlMapper sqlMapper)
- {
- _configService = configService.Value;
- _logger = logger;
- optConfigRedis.Value.Prefix = "";
- optConfigRedis.Value.DefaultDatabase = 7;
- var csredis = new CSRedis.CSRedisClient(optConfigRedis.Value.ToString());
- RedisHelper.Initialization(csredis);
- _sqlMapper = sqlMapper;
- }
-
-
- public async Task<string> GetHealthyDeviceKey(string imei)
- {
- if (string.IsNullOrWhiteSpace(imei)) return null;
- string HealthyDeviceKey = $"Telpol:HealthyDeviceKey:{imei}";
- string keyCache = $"{imei}_HealthyDeviceKey";
- try
- {
- var objCache = MemoryCacheUtil.Get<string>(keyCache);
- if (objCache == null)
- {
- string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
- if (!string.IsNullOrEmpty(deviceKey))
- {
- MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
- return deviceKey;
- }
- }
- return objCache;
- }
- catch (Exception ex)
- {
- _logger.LogError($"GetHealthyDeviceKey,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
- string deviceKey = await RedisHelper.HGetAsync<string>(HealthyDeviceKey, "data");
- if (!string.IsNullOrEmpty(deviceKey))
- {
- MemoryCacheUtil.Set(keyCache, deviceKey, _configService.CacheDurationSeconds10);
- return deviceKey;
- }
- else
- return "";
- }
- }
-
- public async Task<SettingInfosItem> GetManufactorPushSettingHash(string imei, string manufactorId, int dataType)
- {
- if (string.IsNullOrWhiteSpace(manufactorId)) return null;
- string keyCache = $"{manufactorId}_{dataType}_ManufactorPushSettingHash";
- SettingInfosItem settingInfos = null;
- try
- {
- var objCache = MemoryCacheUtil.Get<SettingInfosItem>(keyCache);
- if (objCache == null)
- {
- var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
- if (obj != null)
- {
- if (obj.pushSettings.Any())
- {
- DateTime dt = DateTime.Now;
- var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
- if (settingsItem != null)
- {
- if (settingsItem.settingInfos.Any())
- {
- foreach (var item in settingsItem.settingInfos)
- {
- DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
- DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
-
- if (item.pushFlag && (dt > startTime && dt < endTime))
- {
- settingInfos = item;
- break;
- }
- }
- }
- }
- }
- if (settingInfos != null)
- MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
- else
- MemoryCacheUtil.Remove(keyCache);
- }
- }
- else
- settingInfos = objCache;
- }
- catch (Exception ex)
- {
- _logger.LogError($"GetManufactorPushSettingHash(imei={imei},dataType={dataType}),key={manufactorId},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
- var obj = await RedisHelper.HGetAsync<ManufactorPushSettingInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE_PUSHSITTTIGS, manufactorId);
- if (obj != null)
- {
- if (obj.pushSettings.Any())
- {
- DateTime dt = DateTime.Now;
- var settingsItem = obj.pushSettings.FirstOrDefault<PushSettingsItem>(x => x.dataType == dataType);
- if (settingsItem != null)
- {
- if (settingsItem.settingInfos.Any())
- {
- foreach (var item in settingsItem.settingInfos)
- {
- DateTime startTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushStartTime}");
- DateTime endTime = DateTime.Parse($"{dt.Year}-{dt.Month}-{dt.Day} {item.pushEndTime}");
-
- if (item.pushFlag && (dt > startTime && dt < endTime))
- {
- settingInfos = item;
- break;
- }
- }
- }
- }
- }
- if (settingInfos != null)
- MemoryCacheUtil.Set(keyCache, settingInfos, _configService.CacheDurationSeconds10);
- else
- MemoryCacheUtil.Remove(keyCache);
- }
- }
- return settingInfos;
- }
-
- //public async Task<string> ManufactorKafkaTopicQuery()
- //{
-
- // List<string> topics = new List<string>();
- // string keyCache = $"ManufactorKafkaConfig";
- // try
- // {
- // var objCache = MemoryCacheUtil.Get<List<string>>(keyCache);
- // if (objCache == null)
- // {
- // var obj = await RedisHelper.HGetAsync<List<ManufactorKafkaModel>>(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, imei);
- // if (obj == null)
- // {
- // var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
- // if (manufactors != null)
- // {
- // foreach (var item in manufactors) {
- // if (!string.IsNullOrEmpty(item.kafkaTopic))
- // topics.Add(item.kafkaTopic);
- // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
- // }
- // MemoryCacheUtil.Set(keyCache, topics, _configService.CacheDurationSeconds10);
- // return topics;
- // }
- // }
- // else
- // {
- // foreach (var item in obj)
- // {
- // if (!string.IsNullOrEmpty(item.kafkaTopic))
- // topics.Add(item.kafkaTopic);
- // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
- // }
- // MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds10);
- // return topics;
- // }
- // }
- // return objCache;
- // }
- // catch (Exception ex)
- // {
- // _logger.LogError($"ManufactorKafkaTopicQuery,缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
-
- // var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
- // if (manufactors != null)
- // {
- // foreach (var item in manufactors)
- // {
- // if (!string.IsNullOrEmpty(item.kafkaTopic))
- // topics.Add(item.kafkaTopic);
- // await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, item.manufactorId, item);
- // }
- // MemoryCacheUtil.Set(keyCache, topics, _configService.CacheDurationSeconds10);
- // return topics;
- // }
-
-
- // //var manufactors = _sqlMapper.ManufactorKafkaTopicQuery();
- // //if (manufactors != null)
- // //{
- // // RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_MANUFACTOR_CONFIG, imei, manufactor);
- // // RedisHelper.Set(keyCache, manufactor, _configService.CacheDurationSeconds10);
- // // return manufactor;
- // //}
- // }
- // return null;
- //}
-
- }
- }
|