Browse Source

用户信息缓存增加扩展信息

master
wxd 11 months ago
parent
commit
41e8ddf8fe
  1. 51
      src/Shentun.Peis.Domain/CacheService.cs

51
src/Shentun.Peis.Domain/CacheService.cs

@ -1,9 +1,11 @@
using Microsoft.EntityFrameworkCore;
using AutoMapper.Internal.Mappers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using NPOI.SS.Formula.Functions;
using Shentun.Peis.Enums;
using Shentun.Peis.Models;
using Shentun.Peis.MyUser;
using Shentun.Peis.SysParmValues;
using System;
using System.Collections.Generic;
@ -21,7 +23,7 @@ namespace Shentun.Peis
{
public class CacheService : ISingletonDependency
{
private readonly IDistributedCache<IdentityUser, Guid> _userCache;
private readonly IDistributedCache<IdentityUserWithExtensionDto, Guid> _userCache;
private readonly IMemoryCache _customerOrgCache;
//private readonly IDistributedCache<CustomerOrg, Guid> _customerOrgCache;
private readonly IDistributedCache<string, Guid> _customerOrgTopNameCache;
@ -60,7 +62,7 @@ namespace Shentun.Peis
private readonly IDistributedCache<string, string> _customerOrgDisplayModeCache;
public CacheService(
IDistributedCache<IdentityUser, Guid> userCache,
IDistributedCache<IdentityUserWithExtensionDto, Guid> userCache,
IMemoryCache customerOrgCache,
IDistributedCache<Nation, string> nationCache,
IRepository<IdentityUser, Guid> userRepository,
@ -131,12 +133,44 @@ namespace Shentun.Peis
_diagnosisLevelRepository = diagnosisLevelRepository;
}
private async Task<IdentityUser> GetUserAsync(Guid id)
private async Task<IdentityUserWithExtensionDto> GetUserAsync(Guid id)
{
var entity = await _userCache.GetOrAddAsync(
id, //缓存键
async () => await _userRepository.FirstOrDefaultAsync(m => m.Id == id)
async () =>
{
var dataEntity = await _userRepository.FirstOrDefaultAsync(m => m.Id == id);
var userSign = dataEntity.GetProperty<string>("user_sign");
var userPhoto = dataEntity.GetProperty<string>("user_photo");
var entDto = new IdentityUserWithExtensionDto
{
ConcurrencyStamp = dataEntity.ConcurrencyStamp,
CreationTime = dataEntity.CreationTime,
CreatorId = dataEntity.CreatorId,
DeleterId = dataEntity.DeleterId,
DeletionTime = dataEntity.DeletionTime,
Email = dataEntity.Email,
EmailConfirmed = dataEntity.EmailConfirmed,
Id = dataEntity.Id,
IsActive = dataEntity.IsActive,
IsDeleted = dataEntity.IsDeleted,
LastModificationTime = dataEntity.LastModificationTime,
LastModifierId = dataEntity.LastModifierId,
LockoutEnabled = dataEntity.LockoutEnabled,
LockoutEnd = dataEntity.LockoutEnd,
Name = dataEntity.Name,
PhoneNumber = dataEntity.PhoneNumber,
PhoneNumberConfirmed = dataEntity.PhoneNumberConfirmed,
Surname = dataEntity.Surname,
TenantId = dataEntity.TenantId,
UserName = dataEntity.UserName,
UserPhoto = userPhoto,
UserSign = userSign
};
return entDto;
}
);
return entity;
}
@ -155,14 +189,17 @@ namespace Shentun.Peis
}
public async Task<string> GetUserSignAsync(Guid? id)
{
{
if (id == null || id == default(Guid) || !id.HasValue)
{
return "";
}
var entity = await GetUserAsync((Guid)id);
if (entity != null)
return entity.GetProperty<string>("user_sign");
{
return entity.UserSign;
}
else
return "";
}

Loading…
Cancel
Save