using GpsCardGatewayPosition.Model.Config; 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 TelpoDataService.Util.Clients; using TelpoDataService.Util.Entities.GpsCard; using TelpoDataService.Util.Models; using TelpoDataService.Util.QueryObjects; using TelpoDataService.Util; using GpsCardGatewayPosition.Model.Cache; namespace GpsCardGatewayPosition.Service.Cache { public class PersonCacheManager { private const int DEFAULT_DURATION_SECONDS = 1200; //20分钟 private const string CACHE_KEY_PERSON = "Person_"; //注意要改用 redis DB7 数据库 ,Prefix=TELPO private const string CACHE_HASH_KEY_GPSDEVICEPERSON = "#GPSDEVICE_PERSON_HASH"; private readonly ServiceConfig _configService; private readonly GpsCardAccessorClient _personApiClient; private readonly ILogger _logger; private readonly IOptions _optConfigRedis; public PersonCacheManager(IOptions optConfigService, GpsCardAccessorClient personApiClient, ILogger logger, IOptions optConfigRedis) { _configService = optConfigService.Value; _optConfigRedis = optConfigRedis; _personApiClient = personApiClient; _logger = logger; } public async Task GetPersonBySerialNoAsync(string messageId, string sn) { string key = CACHE_KEY_PERSON + sn; var person = await RedisHelper.GetAsync(key).ConfigureAwait(false); if (person == null) { try { string url = _configService.TelpoDataUrl; var param = new GeneralParam { Filters = new List { new QueryFilterCondition { Key=nameof(GpsPerson.Serialno), Value=sn, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal } } }; person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false); if (person != null) { RedisHelper.SetAsync(key, person, DEFAULT_DURATION_SECONDS); } } catch (Exception ex) { _logger.LogError($"获取查询使用者并更新缓存发生异常:{ex.Message}, {ex.StackTrace}"); } } return person; } /// /// 读取个人信息(血压使用)注意要改用 redis DB7 /// /// /// /// public async Task GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn) { if (string.IsNullOrWhiteSpace(sn)) return null; // 切换redis DB7 数据库和前缀 "TELPO" //_optConfigRedis.Value.DefaultDatabase = 7; //_optConfigRedis.Value.Prefix = "TELPO"; // 增加容错,防止Redis宕机造成业务中断 try { //using (var csRedisDb7 = new CSRedisClient(_optConfigRedis.Value.ToString())) //{ // var person = await csRedisDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn); // return person; //} var person = await RedisHelperDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn); return person; } catch (Exception ex) { _logger.LogWarning($"Redis发生异常,将直接读取MySQL数据库,构造新实例:{ex.Message}, {ex.StackTrace}"); var param = new GeneralParam { Filters = new List { new QueryFilterCondition { Key=nameof(GpsPerson.Serialno), Value=sn, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal } } }; var person = await _personApiClient.GetFirstAsync(param, new RequestHeader { RequestId = messageId }).ConfigureAwait(false); // 读取数据库,构造新实例 return new GpsDevicePerson { Time = DateTime.Now, Person = new Person { DeviceId = person.DeviceId, PersonId = person.PersonId, Remarks = person.Remarks, SerialNo = person.Serialno } }; } } } }