|
- using Microsoft.Extensions.Options;
- using TelpoDataService.Util.Entities.GpsLocationHistory;
- using TelpoPush.WanJiaAn.Worker.Models.CacheTemplates;
- using TelpoPush.WanJiaAn.Worker.Models.Config;
-
- namespace TelpoPush.WanJiaAn.Worker.Service.Cache
- {
- public class RedisUtil
- {
- private const string CACHE_HASH_KEY_TELPO_GPSDEVICE = "TELPO#GPSDEVICE_HASH";
- private const string CACHE_HASH_KEY_TELPO_GPSDEVICE_PERSON = "TELPO#GPSDEVICE_PERSON_HASH";
- 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 const string CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT = "TELPO#WANJIAAN_ALARMEVENT_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<DeviceInfoModel> GetGpsDevice(string imei)
- {
- if (string.IsNullOrWhiteSpace(imei)) return null;
- string keyCache = $"{imei}_GpsDevice";
- try
- {
- var objCache = MemoryCacheUtil.Get<DeviceInfoModel>(keyCache);
- if (objCache == null)
- {
- var obj = await RedisHelper.HGetAsync<DeviceInfoModel>(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei);
- if (obj == null)
- {
- var deviceInfo = _sqlMapper.DeviceInfo(imei);
- if (deviceInfo != null)
- {
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
- MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds10);
- return deviceInfo;
- }
- }
- else
- {
- MemoryCacheUtil.Set(keyCache, obj, _configService.CacheDurationSeconds10);
- return obj;
- }
- }
- return objCache;
- }
- catch (Exception ex)
- {
- _logger.LogError($"GetGpsDevice,key={imei},缓存异常,重新获取数据,{ex.Message}|{ex.Source}|{ex.StackTrace}");
- var deviceInfo = _sqlMapper.DeviceInfo(imei);
- if (deviceInfo != null)
- {
- RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_GPSDEVICE, imei, deviceInfo);
- MemoryCacheUtil.Set(keyCache, deviceInfo, _configService.CacheDurationSeconds10);
- return deviceInfo;
- }
- }
- return null;
- }
-
-
- 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 SetWanjiaanAlarmEvent(string key, WanjiaanAlarmEvent data)
- {
- await RedisHelper.HSetAsync(CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT, key, data);
- }
-
- public async Task DelWanjiaanAlarmEvent(string key)
- {
- await RedisHelper.HDelAsync(CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT, key);
- }
- public async Task<WanjiaanAlarmEvent> GetWanjiaanAlarmEvent(string key)
- {
- return await RedisHelper.HGetAsync<WanjiaanAlarmEvent>(CACHE_HASH_KEY_TELPO_WANJIAAN_ALARMEVENT, key);
- }
- }
- }
|