|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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<GpsPerson> _personApiClient;
- private readonly ILogger<PersonCacheManager> _logger;
- private readonly IOptions<RedisConfig> _optConfigRedis;
-
- public PersonCacheManager(IOptions<ServiceConfig> optConfigService, GpsCardAccessorClient<GpsPerson> personApiClient,
- ILogger<PersonCacheManager> logger, IOptions<RedisConfig> optConfigRedis)
- {
- _configService = optConfigService.Value;
- _optConfigRedis = optConfigRedis;
- _personApiClient = personApiClient;
- _logger = logger;
- }
-
- public async Task<GpsPerson> GetPersonBySerialNoAsync(string messageId, string sn)
- {
- string key = CACHE_KEY_PERSON + sn;
- var person = await RedisHelper.GetAsync<GpsPerson>(key).ConfigureAwait(false);
- if (person == null)
- {
- try
- {
- string url = _configService.TelpoDataUrl;
- var param = new GeneralParam
- {
- Filters = new List<QueryFilterCondition>
- {
- 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;
- }
-
- /// <summary>
- /// 读取个人信息(血压使用)注意要改用 redis DB7
- /// </summary>
- /// <param name="messageId"></param>
- /// <param name="sn"></param>
- /// <returns></returns>
- public async Task<GpsDevicePerson> 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<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn);
- // return person;
- //}
-
- var person = await RedisHelperDb7.HGetAsync<GpsDevicePerson>(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<QueryFilterCondition>
- {
- 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
- }
- };
- }
-
-
- }
- }
- }
|