|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using CSRedis;
- using HealthMonitor.Model.Cache;
- using HealthMonitor.Model.Config;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Net.NetworkInformation;
- using System.Threading.Tasks;
- using TelpoDataService.Util;
- using TelpoDataService.Util.Clients;
- using TelpoDataService.Util.Entities.GpsCard;
- using TelpoDataService.Util.Models;
- using TelpoDataService.Util.QueryObjects;
-
- namespace HealthMonitor.Service.Cache
- {
-
- public class PersonCacheManager
- {
-
-
- private const string CACHE_HASH_KEY_GPSDEVICEPERSON = "#GPSDEVICE_PERSON_HASH";
-
-
-
- private readonly GpsCardAccessorClient<GpsPerson> _personApiClient;
- private readonly ILogger<PersonCacheManager> _logger;
-
-
- public PersonCacheManager(
- GpsCardAccessorClient<GpsPerson> personApiClient,
- ILogger<PersonCacheManager> logger)
- {
- _personApiClient = personApiClient;
- _logger = logger;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public async Task<GpsDevicePerson?> GetDeviceGpsPersonCacheBySerialNoAsync(string messageId, string sn)
- {
- if (string.IsNullOrWhiteSpace(sn)) return null;
-
-
-
- try
- {
- var person = await RedisHelperDb7.HGetAsync<GpsDevicePerson>(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
- 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
- }
- };
- }
-
-
- }
-
-
-
-
-
-
-
- public async Task<JObject?> GetDeviceGpsPersonCacheObjectBySerialNoAsync(string messageId, string sn)
- {
- if (string.IsNullOrWhiteSpace(sn)) return null;
-
- try
- {
- var person = await RedisHelperDb7.HGetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn).ConfigureAwait(false);
- return (JObject?)JsonConvert.DeserializeObject(person);
- }
- catch (Exception ex)
- {
- _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
-
- }
-
- return null;
- }
-
-
-
-
-
-
- public async Task<bool> UpdateDeviceGpsPersonCacheObjectBySerialNoAsync(JObject person, string sn)
- {
- var flag = false;
- if (string.IsNullOrWhiteSpace(sn)) return flag;
- try
- {
-
- await RedisHelperDb7.HSetAsync(CACHE_HASH_KEY_GPSDEVICEPERSON, sn, person).ConfigureAwait(false);
- flag = true;
-
- }
- catch (Exception ex)
- {
- _logger.LogWarning($"Redis发生异常:{ex.Message}, {ex.StackTrace}");
- }
- return flag;
-
- }
- }
- }
|