添加项目文件。
This commit is contained in:
63
Atomx.Data/CacheServices/AgencyCacheService.cs
Normal file
63
Atomx.Data/CacheServices/AgencyCacheService.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取代理信息
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<Agency> GetAgencyById(long id, bool? reload = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取代理的客服信息
|
||||
// /// </summary>
|
||||
// /// <param name="agencyId"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<CustomerService>> GetCustomerServices(long agencyId, bool? reload = null);
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取代理信息
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<Agency> GetAgencyById(long id, bool? reload = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<Agency>($"{CacheKeys.AgencyPrefix}{id}");
|
||||
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.Agencies.SingleOrDefault(p => p.Id == id);
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync($"{CacheKeys.AgencyPrefix}{id}", cacheData);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task<List<CustomerService>> GetCustomerServices(long agencyId, bool? reload = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<CustomerService>>($"{CacheKeys.CustomerServicePrefix}{agencyId}");
|
||||
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.CustomerServices.Where(p => p.Enabled && p.AgencyId == agencyId).ToList();
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync($"{CacheKeys.CustomerServicePrefix}{agencyId}", cacheData);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
97
Atomx.Data/CacheServices/AreaCacheService.cs
Normal file
97
Atomx.Data/CacheServices/AreaCacheService.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取国家数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<List<Area>> GetCountry(bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取国家名称
|
||||
/// </summary>
|
||||
/// <param name="countryId"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> GetCountryName(long countryId);
|
||||
|
||||
/// <summary>
|
||||
/// 根据上级ID获取下级数据
|
||||
/// </summary>
|
||||
/// <param name="parentId"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Area>> GetAreas(long parentId, bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 更新调整缓存数据
|
||||
/// </summary>
|
||||
/// <param name="area"></param>
|
||||
/// <returns></returns>
|
||||
Task ResetArea(Area area);
|
||||
}
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
public async Task<List<Area>> GetCountry(bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
var cacheData = await GetCacheAsync<List<Area>>(CacheKeys.Country);
|
||||
if(cacheData == null || reloadData)
|
||||
{
|
||||
var countries = (from p in _dbContext.Areas
|
||||
where p.ParentId == 0
|
||||
select p).ToList();
|
||||
await SetCacheAsync(CacheKeys.Country, countries);
|
||||
return countries;
|
||||
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
public async Task<string> GetCountryName(long countryId)
|
||||
{
|
||||
var cacheData = await GetCountry();
|
||||
var data = cacheData.SingleOrDefault(p => p.Id == countryId);
|
||||
if (data == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return data.Name;
|
||||
}
|
||||
|
||||
public async Task<List<Area>> GetAreas(long parentId, bool? reload = false)
|
||||
{
|
||||
if (parentId == 0)
|
||||
{
|
||||
return await GetCountry();
|
||||
}
|
||||
var cacheData = await GetCacheAsync<List<Area>>($"{CacheKeys.Area}.{parentId}");
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var data = (from p in _dbContext.Areas
|
||||
where p.ParentId == 0
|
||||
select p).ToList();
|
||||
await SetCacheAsync(CacheKeys.Country, data);
|
||||
return data;
|
||||
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
public async Task ResetArea(Area area)
|
||||
{
|
||||
var cacheData = await GetAreas(area.ParentId);
|
||||
var data = cacheData.Where(p => p.Id == area.Id).SingleOrDefault();
|
||||
if (data != null)
|
||||
{
|
||||
cacheData.Remove(data);
|
||||
}
|
||||
cacheData.Add(area);
|
||||
await SetCacheAsync($"{CacheKeys.Area}.{area.ParentId}", cacheData);
|
||||
}
|
||||
}
|
||||
}
|
||||
253
Atomx.Data/CacheServices/AttributeCacheService.cs
Normal file
253
Atomx.Data/CacheServices/AttributeCacheService.cs
Normal file
@@ -0,0 +1,253 @@
|
||||
//using Atomx.Common.Entities;
|
||||
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取产品属性值缓存列表
|
||||
// /// </summary>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<ProductAttribute>> GetProductAttributes(List<ProductAttribute>? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取或更新缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<ProductAttribute> GetProductAttributeById(long id, ProductAttribute? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 从缓存中删除数据
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemoveProductAttribute(long id);
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取产品预设值
|
||||
// /// </summary>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<ProductAttributeOption>> GetProductAttributeOptions(long attributeId, List<ProductAttributeOption>? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取或更新缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<ProductAttributeOption> GetProductAttributeOptionById(long attributeId, long id, ProductAttributeOption? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 从缓存中删除数据
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemoveProductAttributeOption(long attributeId, long id);
|
||||
|
||||
// /// <summary>
|
||||
// /// 根据ID获取属性树结构
|
||||
// /// </summary>
|
||||
// /// <param name="attributeId"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<AttributeTreeModel> GetAttributeTree(long attributeId);
|
||||
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// public async Task<List<ProductAttribute>> GetProductAttributes(List<ProductAttribute>? data = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<ProductAttribute>>(CacheKeys.ProductAttributes);
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync(CacheKeys.ProductAttributes, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new List<ProductAttribute>();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task<ProductAttribute> GetProductAttributeById(long id, ProductAttribute? data = null)
|
||||
// {
|
||||
// var cacheData = await GetProductAttributes();
|
||||
// if (data != null)
|
||||
// {
|
||||
// var attribute = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (attribute != null)
|
||||
// {
|
||||
// cacheData.Remove(attribute);
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.ProductAttributes, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.ProductAttributes, cacheData);
|
||||
// }
|
||||
// return data;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var attribute = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (attribute != null)
|
||||
// {
|
||||
// return attribute;
|
||||
// }
|
||||
// return new ProductAttribute();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// public async Task RemoveProductAttribute(long id)
|
||||
// {
|
||||
// var cacheData = await GetProductAttributes();
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData.Remove(data);
|
||||
// await SetCacheAsync(CacheKeys.ProductAttributes, cacheData);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取产品预设值
|
||||
// /// </summary>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<ProductAttributeOption>> GetProductAttributeOptions(long attributeId, List<ProductAttributeOption>? data = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<ProductAttributeOption>>($"{CacheKeys.ProductAttributeOptionsPrefix}{attributeId}");
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync($"{CacheKeys.ProductAttributeOptionsPrefix}{attributeId}", cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new List<ProductAttributeOption>();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取或更新缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<ProductAttributeOption> GetProductAttributeOptionById(long attributeId, long id, ProductAttributeOption? data = null)
|
||||
// {
|
||||
// var cacheData = await GetProductAttributeOptions(attributeId);
|
||||
// if (data != null)
|
||||
// {
|
||||
// var attribute = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (attribute != null)
|
||||
// {
|
||||
// cacheData.Remove(attribute);
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync($"{CacheKeys.ProductAttributeOptionsPrefix}{attributeId}", cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync($"{CacheKeys.ProductAttributeOptionsPrefix}{attributeId}", cacheData);
|
||||
// }
|
||||
// return data;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var attribute = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (attribute != null)
|
||||
// {
|
||||
// return attribute;
|
||||
// }
|
||||
// return new ProductAttributeOption();
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 从缓存中删除数据
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task RemoveProductAttributeOption(long attributeId, long id)
|
||||
// {
|
||||
// var cacheData = await GetProductAttributeOptions(attributeId);
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData.Remove(data);
|
||||
// await SetCacheAsync($"{CacheKeys.ProductAttributeOptionsPrefix}{attributeId}", cacheData);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取属性结构缓存
|
||||
// /// </summary>
|
||||
// /// <param name="attributeId"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<AttributeTreeModel> GetAttributeTree(long attributeId)
|
||||
// {
|
||||
// var attribute = await GetProductAttributeById(attributeId);
|
||||
// var options = await GetProductAttributeOptions(attributeId);
|
||||
|
||||
// if (attribute.Id ==0)
|
||||
// {
|
||||
// attribute = _dbContext.ProductAttributes.SingleOrDefault(p => p.Id == attributeId);
|
||||
// if (attribute != null)
|
||||
// {
|
||||
// await GetProductAttributeById(attribute.Id, attribute);
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (attribute == null)
|
||||
// {
|
||||
// return new AttributeTreeModel();
|
||||
// }
|
||||
|
||||
// if (!options.Any())
|
||||
// {
|
||||
// options = _dbContext.ProductAttributeOptions.Where(p => p.AttributeId == attributeId).ToList();
|
||||
// if (options.Any())
|
||||
// {
|
||||
// await GetProductAttributeOptions(attribute.Id, options);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// var data = new AttributeTreeModel()
|
||||
// {
|
||||
// Id = attribute.Id,
|
||||
// Name = attribute.Name,
|
||||
// ProductTypeId = attribute.ProductTypeId,
|
||||
// PredefinedValues = options
|
||||
// };
|
||||
// return data;
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
46
Atomx.Data/CacheServices/BasicCacheService.cs
Normal file
46
Atomx.Data/CacheServices/BasicCacheService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取行为日志类型,自动初始化
|
||||
// /// </summary>
|
||||
// /// <param name="reload">是否重载</param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<ActionLogType>> GetActionLogTypes(bool reload = false);
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取行为日志类型
|
||||
// /// </summary>
|
||||
// /// <param name="reload">是否重载</param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<ActionLogType>> GetActionLogTypeList(List<ActionLogType>? data = null);
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// public async Task<List<ActionLogType>> GetActionLogTypes(bool reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<ActionLogType>>(CacheKeys.ActionLogType);
|
||||
// if (cacheData == null || reload)
|
||||
// {
|
||||
// cacheData = _dbContext.ActionLogTypes.ToList();
|
||||
// await SetCacheAsync(CacheKeys.ActionLogType, cacheData);
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task<List<ActionLogType>> GetActionLogTypeList(List<ActionLogType>? data = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<ActionLogType>>(CacheKeys.ActionLogType);
|
||||
// if (cacheData == null || data != null)
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync(CacheKeys.ActionLogType, cacheData);
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
176
Atomx.Data/CacheServices/CacheService.cs
Normal file
176
Atomx.Data/CacheServices/CacheService.cs
Normal file
@@ -0,0 +1,176 @@
|
||||
using Atomx.Data;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="time">缓存时间,默认8小时</param>
|
||||
/// <returns></returns>
|
||||
Task SetCacheAsync<T>(string key, T value, double time = 8);
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定KEY获取数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
Task<T?> GetCacheAsync<T>(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定 key 的缓存List数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<T>> GetCacheList<T>(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 清除指定 key 的缓存
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
Task Remove(string key);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
readonly DataContext _dbContext;
|
||||
readonly IDistributedCache _cache;
|
||||
|
||||
public CacheService(DataContext dataContext, IDistributedCache distributedCache)
|
||||
{
|
||||
_dbContext = dataContext;
|
||||
_cache = distributedCache;
|
||||
}
|
||||
|
||||
|
||||
public Task Remove(string key)
|
||||
{
|
||||
_cache.Remove(key);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
public T? GetCache<T>(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cacheData = _cache.GetString(key);
|
||||
if (cacheData != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(cacheData);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"无法链接缓存服务,读取缓存数据失败,错误信息:{ex.Message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task<T?> GetCacheAsync<T>(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cacheData = await _cache.GetStringAsync(key);
|
||||
if (cacheData != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(cacheData);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"无法链接缓存服务,读取缓存数据失败,错误信息:{ex.Message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public async Task<List<T>> GetCacheList<T>(string key)
|
||||
{
|
||||
var cacheData = await _cache.GetStringAsync(key);
|
||||
if (cacheData != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var data = JsonSerializer.Deserialize<List<T>>(cacheData);
|
||||
if (data != null)
|
||||
{
|
||||
return data;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<T>();
|
||||
}
|
||||
}
|
||||
return new List<T>();
|
||||
}
|
||||
|
||||
public void SetCache<T>(string key, T value, double time = 8)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions();
|
||||
var minute = time * 60;
|
||||
var timspace = TimeSpan.FromMinutes(minute);
|
||||
options.SetSlidingExpiration(timspace);
|
||||
|
||||
try
|
||||
{
|
||||
_cache.SetString(key, JsonSerializer.Serialize(value), options);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"无法链接缓存服务,写入缓存数据失败,错误信息:{ex.Message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetCacheAsync<T>(string key, T value, double time = 8)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions();
|
||||
var minute = time * 60;
|
||||
var timspace = TimeSpan.FromMinutes(minute);
|
||||
options.SetSlidingExpiration(timspace);
|
||||
|
||||
try
|
||||
{
|
||||
await _cache.SetStringAsync(key, JsonSerializer.Serialize(value), options);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"无法链接缓存服务,写入缓存数据失败,错误信息:{ex.Message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
300
Atomx.Data/CacheServices/CategoryCacheService.cs
Normal file
300
Atomx.Data/CacheServices/CategoryCacheService.cs
Normal file
@@ -0,0 +1,300 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
using Atomx.Common.Enums;
|
||||
using Atomx.Common.Models;
|
||||
using Atomx.Utils.Extension;
|
||||
using Mapster;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取内容分类列表
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<CategoryItemModel>> GetContentCategoryList(bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取产品分类列表
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<CategoryItemModel>> GetProductCategoryList(bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取可用的缓存信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<List<Category>> GetContentCategories(List<Category>? data = null);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取或更新缓存
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
Task<Category> GetContentCategoryById(long id, Category? data = null);
|
||||
|
||||
/// <summary>
|
||||
/// 从缓存中移除分类缓存
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task RemoveContentCategory(long id);
|
||||
|
||||
/// <summary>
|
||||
/// 获取可用的缓存信息
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Category>> GetProductCategories(bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取或更新缓存
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
Task<Category> GetProductCategoryById(long id, Category? data = null);
|
||||
|
||||
/// <summary>
|
||||
/// 重缓存中移除产品分类
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task RemoveProductCategory(long id);
|
||||
}
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取内容分类列表
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<CategoryItemModel>> GetContentCategoryList(bool? reload = false)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<List<CategoryItemModel>>(CacheKeys.ContentCategories);
|
||||
bool needReload = reload.HasValue ? reload.Value : false;
|
||||
if (cacheData == null || needReload)
|
||||
{
|
||||
var category = _dbContext.Categories.Where(p => p.Type == (int)CategoryType.Content).ToList();
|
||||
var list = new List<CategoryItemModel>();
|
||||
cacheData = BuildCategoryList(list, category, 0);
|
||||
await SetCacheAsync(CacheKeys.ContentCategories, cacheData);
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取产品分类列表
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<CategoryItemModel>> GetProductCategoryList(bool? reload = false)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<List<CategoryItemModel>>(CacheKeys.ProductCategories);
|
||||
bool needReload = reload.HasValue ? reload.Value : false;
|
||||
if (cacheData == null || needReload)
|
||||
{
|
||||
var category = _dbContext.Categories.Where(p => p.Type == (int)CategoryType.Product).ToList();
|
||||
var list = new List<CategoryItemModel>();
|
||||
cacheData = BuildCategoryList(list, category, 0);
|
||||
await SetCacheAsync(CacheKeys.ProductCategories, cacheData);
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
List<CategoryItemModel> BuildCategoryList(List<CategoryItemModel> list, List<Category> data, long parentId)
|
||||
{
|
||||
var categories = data.Where(p => p.ParentId == parentId).OrderBy(p => p.DisplayOrder).ToList();
|
||||
foreach (var item in categories)
|
||||
{
|
||||
var category = item.Adapt<CategoryItemModel>();
|
||||
|
||||
if (!string.IsNullOrEmpty(item.Path))
|
||||
{
|
||||
var ids = item.Path.Split(',').Select(p => p.ToLong()).ToList();
|
||||
var breadcrumbs = string.Empty;
|
||||
foreach (var id in ids)
|
||||
{
|
||||
var name = data.Where(p => p.Id == id).Select(p => p.Name).SingleOrDefault();
|
||||
if (!string.IsNullOrEmpty(breadcrumbs))
|
||||
{
|
||||
breadcrumbs = breadcrumbs + " >> " + name;
|
||||
}
|
||||
else
|
||||
{
|
||||
breadcrumbs = name;
|
||||
}
|
||||
}
|
||||
|
||||
category.Breadcrumbs = breadcrumbs;
|
||||
category.Breadcrumbs = breadcrumbs + " >> " + category.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
category.Breadcrumbs = item.Name;
|
||||
}
|
||||
list.Add(category);
|
||||
if (data.Count(p => p.ParentId == item.Id) > 0)
|
||||
{
|
||||
List<CategoryItemModel> childlist = new List<CategoryItemModel>();
|
||||
childlist = BuildCategoryList(childlist, data, item.Id);
|
||||
list.AddRange(childlist);
|
||||
}
|
||||
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public async Task<Category> GetProductCategoryById(long id, Category? data = null)
|
||||
{
|
||||
var cacheData = await GetProductCategories();
|
||||
if (data != null)
|
||||
{
|
||||
var category = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (category != null)
|
||||
{
|
||||
cacheData.Remove(category);
|
||||
cacheData.Add(data);
|
||||
await SetCacheAsync(CacheKeys.ProductCategories, cacheData);
|
||||
}
|
||||
else
|
||||
{
|
||||
cacheData.Add(data);
|
||||
await SetCacheAsync(CacheKeys.ProductCategories, cacheData);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
else
|
||||
{
|
||||
var category = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (category == null)
|
||||
{
|
||||
category = _dbContext.Categories.SingleOrDefault(p => p.Id == id);
|
||||
if (category != null)
|
||||
{
|
||||
cacheData.Add(category);
|
||||
await SetCacheAsync(CacheKeys.ProductCategories, cacheData);
|
||||
}
|
||||
}
|
||||
return category;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveProductCategory(long id)
|
||||
{
|
||||
var cacheData = await GetProductCategories();
|
||||
if (cacheData != null)
|
||||
{
|
||||
var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (data != null)
|
||||
{
|
||||
cacheData.Remove(data);
|
||||
await SetCacheAsync(CacheKeys.ProductCategories, cacheData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Category>> GetContentCategories(List<Category>? data = null)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<List<Category>>(CacheKeys.ContentCategories);
|
||||
if (data != null)
|
||||
{
|
||||
cacheData = data;
|
||||
await SetCacheAsync(CacheKeys.ContentCategories, cacheData);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cacheData == null)
|
||||
{
|
||||
return new List<Category>();
|
||||
}
|
||||
}
|
||||
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取可用的缓存信息
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Category>> GetProductCategories(bool? reload = false)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<List<Category>>(CacheKeys.ProductCategories);
|
||||
bool needReload = reload.HasValue ? reload.Value : false;
|
||||
if (cacheData == null || needReload)
|
||||
{
|
||||
var data = _dbContext.Categories.Where(p => p.Type == (int)CategoryType.Product && p.Enabled).ToList();
|
||||
|
||||
cacheData = data;
|
||||
|
||||
await SetCacheAsync(CacheKeys.ProductCategories, cacheData);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cacheData == null)
|
||||
{
|
||||
return new List<Category>();
|
||||
}
|
||||
}
|
||||
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
public async Task<Category> GetContentCategoryById(long id, Category? data = null)
|
||||
{
|
||||
var cacheData = await GetContentCategories();
|
||||
if (data != null)
|
||||
{
|
||||
var category = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (category != null)
|
||||
{
|
||||
cacheData.Remove(category);
|
||||
cacheData.Add(data);
|
||||
await SetCacheAsync(CacheKeys.ContentCategories, cacheData);
|
||||
}
|
||||
else
|
||||
{
|
||||
cacheData.Add(data);
|
||||
await SetCacheAsync(CacheKeys.ContentCategories, cacheData);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
else
|
||||
{
|
||||
var category = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (category == null)
|
||||
{
|
||||
category = _dbContext.Categories.SingleOrDefault(p => p.Id == id);
|
||||
if (category != null)
|
||||
{
|
||||
cacheData.Add(category);
|
||||
await SetCacheAsync(CacheKeys.ContentCategories, cacheData);
|
||||
}
|
||||
}
|
||||
return category;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveContentCategory(long id)
|
||||
{
|
||||
var cacheData = await GetContentCategories();
|
||||
if (cacheData != null)
|
||||
{
|
||||
var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (data != null)
|
||||
{
|
||||
cacheData.Remove(data);
|
||||
await SetCacheAsync(CacheKeys.ContentCategories, cacheData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
320
Atomx.Data/CacheServices/ConfigCacheService.cs
Normal file
320
Atomx.Data/CacheServices/ConfigCacheService.cs
Normal file
@@ -0,0 +1,320 @@
|
||||
//using Atomx.Common.Entities;
|
||||
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取管理员菜单系统
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<PermissionRecord>> GetAdminMenu(List<PermissionRecord>? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取角色信息
|
||||
// /// </summary>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Role>> GetRoles(Role? data = null, bool remove = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取基本配置信息
|
||||
// /// </summary>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<GeneralConfig> GetGeneralConfig(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取系统默认语言ID
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<int> GetDefaultLanguageId();
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取系统默认语言
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<string> GetDefaultLanguage();
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取货币ID
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<int> GetCurrencyId();
|
||||
|
||||
// /// <summary>
|
||||
// /// 货币代码
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<string> GetCurrency();
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取邮件配置
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<EmailConfig> GetEmailConfig(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取忘记交易密码邮件内容模板
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<EmailTemplateConfig> GetForgetTransactionPasswordEmailTemplate(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取忘记交易密码邮件内容模板
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<EmailTemplateConfig> GetForgetPasswordEmailTemplate(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 账号激活邮件内容模板
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<EmailTemplateConfig> GetRegisterActiveEmailTemplate(bool? reload = false);
|
||||
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取管理员菜单系统
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<PermissionRecord>> GetAdminMenu(List<PermissionRecord>? data = null)
|
||||
// {
|
||||
|
||||
// var cacheData = await GetCacheAsync<List<PermissionRecord>>(CacheKeys.AdminMenu);
|
||||
// if (data != null)
|
||||
// {
|
||||
// await SetCacheAsync(CacheKeys.AdminMenu, data);
|
||||
// cacheData = data;
|
||||
// }
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new List<PermissionRecord>();
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取基础缓存信息
|
||||
// /// </summary>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// /// <exception cref="NotImplementedException"></exception>
|
||||
// public async Task<GeneralConfig> GetGeneralConfig(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<GeneralConfig>(CacheKeys.GeneralConfig);
|
||||
// var needReload = reload.Value;
|
||||
|
||||
// if (cacheData == null || needReload)
|
||||
// {
|
||||
// var config = _dbContext.Configs.SingleOrDefault(p => p.Key == ConfigKeys.General);
|
||||
// if (config != null)
|
||||
// {
|
||||
// cacheData = config.Content.FromJson<GeneralConfig>();
|
||||
// await SetCacheAsync(CacheKeys.GeneralConfig, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return new GeneralConfig();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取角色得信息
|
||||
// /// </summary>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Role>> GetRoles(Role? data = null, bool remove = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<Role>>(CacheKeys.Roles);
|
||||
// if (data != null)
|
||||
// {
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// var role = cacheData.SingleOrDefault(p => p.Id == data.Id);
|
||||
// if (role != null)
|
||||
// {
|
||||
// cacheData.Remove(role);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cacheData = new List<Role>();
|
||||
|
||||
// }
|
||||
// if (!remove)
|
||||
// {
|
||||
// cacheData.Add(data);
|
||||
// }
|
||||
// await SetCacheAsync(CacheKeys.Roles, cacheData);
|
||||
// }
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new List<Role>();
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取系统默认语言ID
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<int> GetDefaultLanguageId()
|
||||
// {
|
||||
// var data = await GetGeneralConfig();
|
||||
// return data.LanguageId;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取系统默认语言
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<string> GetDefaultLanguage()
|
||||
// {
|
||||
// var data = await GetGeneralConfig();
|
||||
// return data.Language;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取货币ID
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<int> GetCurrencyId()
|
||||
// {
|
||||
// var data = await GetGeneralConfig();
|
||||
// return data.PrimaryCurrencyId;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 货币代码
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<string> GetCurrency()
|
||||
// {
|
||||
// var data = await GetGeneralConfig();
|
||||
// return data.PrimaryCurrency;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取邮件服务配置
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<EmailConfig> GetEmailConfig(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<EmailConfig>(CacheKeys.EmailConfig);
|
||||
// var needReload = reload.Value;
|
||||
|
||||
// if (cacheData == null || needReload)
|
||||
// {
|
||||
// var config = _dbContext.Configs.SingleOrDefault(p => p.Key == ConfigKeys.EmailServer);
|
||||
// if (config != null)
|
||||
// {
|
||||
// cacheData = config.Content.FromJson<EmailConfig>();
|
||||
// await SetCacheAsync(CacheKeys.EmailConfig, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return new EmailConfig();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取忘记交易密码邮件内容模板
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<EmailTemplateConfig> GetForgetTransactionPasswordEmailTemplate(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<EmailTemplateConfig>(CacheKeys.ForgetTransactionPasswordEmailTemplateConfig);
|
||||
// var needReload = reload.Value;
|
||||
|
||||
// if (cacheData == null || needReload)
|
||||
// {
|
||||
// var config = _dbContext.Configs.SingleOrDefault(p => p.Key == ConfigKeys.ForgetTransactionPasswordEmailTemplate);
|
||||
// if (config != null)
|
||||
// {
|
||||
// cacheData = config.Content.FromJson<EmailTemplateConfig>();
|
||||
// await SetCacheAsync(CacheKeys.ForgetTransactionPasswordEmailTemplateConfig, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return new EmailTemplateConfig();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取忘记交易密码邮件内容模板
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<EmailTemplateConfig> GetForgetPasswordEmailTemplate(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<EmailTemplateConfig>(CacheKeys.ForgetPasswordEmailTemplateConfig);
|
||||
// var needReload = reload.Value;
|
||||
|
||||
// if (cacheData == null || needReload)
|
||||
// {
|
||||
// var config = _dbContext.Configs.SingleOrDefault(p => p.Key == ConfigKeys.ForgetPasswordEmailTemplate);
|
||||
// if (config != null)
|
||||
// {
|
||||
// cacheData = config.Content.FromJson<EmailTemplateConfig>();
|
||||
// await SetCacheAsync(CacheKeys.ForgetPasswordEmailTemplateConfig, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return new EmailTemplateConfig();
|
||||
// }
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取忘记交易密码邮件内容模板
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<EmailTemplateConfig> GetRegisterActiveEmailTemplate(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<EmailTemplateConfig>(CacheKeys.RegisterActiveEmailTemplateConfig);
|
||||
// var needReload = reload.Value;
|
||||
|
||||
// if (cacheData == null || needReload)
|
||||
// {
|
||||
// var config = _dbContext.Configs.SingleOrDefault(p => p.Key == ConfigKeys.RegisterEmailActiveTemplate);
|
||||
// if (config != null)
|
||||
// {
|
||||
// cacheData = config.Content.FromJson<EmailTemplateConfig>();
|
||||
// await SetCacheAsync(CacheKeys.RegisterActiveEmailTemplateConfig, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return new EmailTemplateConfig();
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
84
Atomx.Data/CacheServices/ContentCacheService.cs
Normal file
84
Atomx.Data/CacheServices/ContentCacheService.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 通过 slug 获取缓存中的页面主题信息
|
||||
// /// </summary>
|
||||
// /// <param name="slug"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<PageContentModel?> GetPage(string slug, PageContentModel? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取缓存
|
||||
// /// </summary>
|
||||
// /// <param name="slug"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<PageContentModel?> GetContent(string slug, bool? reload = null);
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// public async Task<PageContentModel?> GetPage(string slug, PageContentModel? data = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<PageContentModel>($"{CacheKeys.ContentPrefix}{slug}");
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync($"{CacheKeys.ContentPrefix}{slug}", cacheData);
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<PageContentModel?> GetContent(string slug, bool? reload = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<PageContentModel>($"{CacheKeys.ContentPrefix}{slug}");
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var content = (from p in _dbContext.Contents
|
||||
// where p.Slug == slug && p.Status == (int)ContentStatus.Publish
|
||||
// select new PageContentModel
|
||||
// {
|
||||
// AllowComment = p.AllowComment,
|
||||
// Status = p.Status,
|
||||
// Body = p.Body,
|
||||
// CategoryId = p.CategoryId,
|
||||
// CreateTime = p.CreateTime,
|
||||
// Description = p.Description,
|
||||
// Id = p.Id,
|
||||
// Localized = new(),
|
||||
// IsTop = p.IsTop,
|
||||
// LanguageId = p.LanguageId,
|
||||
// MetaDescription = p.MetaDescription,
|
||||
// MetaTitle = p.MetaTitle,
|
||||
// PublishTime = p.PublishTime,
|
||||
// Slug = p.Slug,
|
||||
// Title = p.Title,
|
||||
// Type = p.Type,
|
||||
// UpdateTime = p.UpdateTime,
|
||||
// UserId = p.UserId,
|
||||
// }).SingleOrDefault();
|
||||
// if (content != null)
|
||||
// {
|
||||
// var Localization = _dbContext.LocalizedProperties.Where(p => p.EntityId == content.Id).ToList();
|
||||
|
||||
// content.Localized = Localization;
|
||||
|
||||
// cacheData = content;
|
||||
|
||||
// await SetCacheAsync($"{CacheKeys.ContentPrefix}{slug}", cacheData);
|
||||
// }
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
189
Atomx.Data/CacheServices/CurrencyCacheService.cs
Normal file
189
Atomx.Data/CacheServices/CurrencyCacheService.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
//public partial interface ICacheService
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// 获取可付款的货币类型和支付通道信息
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<CurrencyChannelModel> GetCurrencyChannel(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取支持充值的货币信息
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Currency>> GetDepositCurrencies();
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取所有可用的货币信息
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Currency>?> GetCurrencies(bool? reload = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取或更新缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<Currency> GetCurrenciesById(long id, Currency? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 根据代码获取货币信息
|
||||
// /// </summary>
|
||||
// /// <param name="code"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<Currency> GetCurrency(string code);
|
||||
|
||||
// /// <summary>
|
||||
// /// 从缓存中删除数据
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemoveCurrencies(long id);
|
||||
|
||||
//}
|
||||
//public partial class CacheService : ICacheService
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// 获取可付款的货币类型和支付通道信息
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<CurrencyChannelModel> GetCurrencyChannel(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<CurrencyChannelModel>(CacheKeys.CurrencyChannel);
|
||||
// bool needReload = reload.HasValue ? reload.Value : false;
|
||||
// if (cacheData == null || needReload)
|
||||
// {
|
||||
// cacheData = new CurrencyChannelModel();
|
||||
|
||||
// var relation = _dbContext.CurrencyChannelRelations.ToList();
|
||||
// var currency = _dbContext.Currencies.Where(p => p.EnablePay).ToList();
|
||||
// var channels = _dbContext.PaymentChannels.Where(p => p.Status == (int)PaymentChannelStatus.Enable).ToList();
|
||||
|
||||
// cacheData.Currencies = currency;
|
||||
// foreach (var item in relation)
|
||||
// {
|
||||
// var channel = channels.SingleOrDefault(p => p.Id == item.PayChannelId);
|
||||
// if (channel != null)
|
||||
// {
|
||||
// var data = new CurrencyPaymentChannelModel()
|
||||
// {
|
||||
// Id = channel.Id,
|
||||
// Config = channel.Config,
|
||||
// CreateTime = channel.CreateTime,
|
||||
// Description = channel.Description,
|
||||
// DisplayOrder = channel.DisplayOrder,
|
||||
// EndTime = item.EndTime,
|
||||
// Extended = item.Extended,
|
||||
// Name = channel.Name,
|
||||
// Network = channel.Network,
|
||||
// StartTime = item.StartTime,
|
||||
// Status = channel.Status,
|
||||
// TimeZone = item.TimeZone,
|
||||
// Type = channel.Type,
|
||||
// UpdateTime = channel.UpdateTime,
|
||||
// CurrencyId = item.CurrencyId,
|
||||
// Account = channel.Account,
|
||||
// Title = channel.Title,
|
||||
// };
|
||||
// cacheData.CurrencyPaymentChannels.Add(data);
|
||||
|
||||
// }
|
||||
// }
|
||||
// await SetCacheAsync(CacheKeys.CurrencyChannel, cacheData);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取支持充值的货币信息
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Currency>> GetDepositCurrencies()
|
||||
// {
|
||||
// var data = await GetCurrencies();
|
||||
// return data.Where(p => p.EnablePay).ToList();
|
||||
// }
|
||||
|
||||
// public async Task<List<Currency>?> GetCurrencies(bool? reload = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<Currency>>(CacheKeys.Currencies);
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.Currencies.Where(p => p.Enabled).ToList();
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync(CacheKeys.Currencies, cacheData);
|
||||
// }
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task<Currency> GetCurrenciesById(long id, Currency? data = null)
|
||||
// {
|
||||
// var cacheData = await GetCurrencies();
|
||||
// if (data != null)
|
||||
// {
|
||||
// var manufacturer = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (manufacturer != null)
|
||||
// {
|
||||
// cacheData.Remove(manufacturer);
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.Currencies, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.Currencies, cacheData);
|
||||
// }
|
||||
// return data;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var manufacturer = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (manufacturer != null)
|
||||
// {
|
||||
// return manufacturer;
|
||||
// }
|
||||
// return new Currency();
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 根据代码获取货币信息
|
||||
// /// </summary>
|
||||
// /// <param name="code"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<Currency> GetCurrency(string code)
|
||||
// {
|
||||
// var cacheData = await GetCurrencies();
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// return cacheData.SingleOrDefault(p => p.CurrencyCode == code);
|
||||
// }
|
||||
// return new Currency();
|
||||
// }
|
||||
|
||||
// public async Task RemoveCurrencies(long id)
|
||||
// {
|
||||
// var cacheData = await GetCurrencies();
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData.Remove(data);
|
||||
// await SetCacheAsync(CacheKeys.Currencies, cacheData);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
103
Atomx.Data/CacheServices/LanguageCacheService.cs
Normal file
103
Atomx.Data/CacheServices/LanguageCacheService.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取语言信息
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Language>> GetLanguages(bool? reload = null);
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取或更新缓存
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
Task<Language> GetLanguageById(long id, Language? data = null);
|
||||
|
||||
/// <summary>
|
||||
/// 通过代码获取语言
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
Task<Language> GetLanguageByCulture(string culture);
|
||||
}
|
||||
|
||||
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
public async Task<List<Language>> GetLanguages(bool? reload = null)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<List<Language>>(CacheKeys.Languages);
|
||||
if (cacheData == null || reload.HasValue)
|
||||
{
|
||||
var data = _dbContext.Languages.Where(p => p.Enabled).ToList();
|
||||
if (data != null)
|
||||
{
|
||||
cacheData = data;
|
||||
await SetCacheAsync(CacheKeys.Languages, cacheData);
|
||||
}
|
||||
}
|
||||
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
public async Task<Language> GetLanguageById(long id, Language? data = null)
|
||||
{
|
||||
var cacheData = await GetLanguages();
|
||||
if (data != null)
|
||||
{
|
||||
var language = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (language != null)
|
||||
{
|
||||
cacheData.Remove(language);
|
||||
cacheData.Add(data);
|
||||
await SetCacheAsync(CacheKeys.Languages, cacheData);
|
||||
}
|
||||
else
|
||||
{
|
||||
cacheData.Add(data);
|
||||
await SetCacheAsync(CacheKeys.Languages, cacheData);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
else
|
||||
{
|
||||
var language = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
if (language != null)
|
||||
{
|
||||
return language;
|
||||
}
|
||||
return new Language();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过代码获取语言
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Language> GetLanguageByCulture(string culture)
|
||||
{
|
||||
var cacheData = await GetLanguages();
|
||||
|
||||
var language = cacheData.SingleOrDefault(p => p.Culture == culture);
|
||||
if (language == null)
|
||||
{
|
||||
language = _dbContext.Languages.SingleOrDefault(p => p.Culture == culture);
|
||||
if (language != null)
|
||||
{
|
||||
cacheData.Add(language);
|
||||
await SetCacheAsync(CacheKeys.Languages, cacheData);
|
||||
}
|
||||
}
|
||||
return language;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
183
Atomx.Data/CacheServices/LocaleResourceCacheService.cs
Normal file
183
Atomx.Data/CacheServices/LocaleResourceCacheService.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过文化名城获取对应的本地化译文
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
Task<Dictionary<string, string>> GetLocaleResourcesAsync(string culture);
|
||||
|
||||
/// <summary>
|
||||
/// 添加或更新文化本地化信息
|
||||
/// </summary>
|
||||
/// <param name="localeResource"></param>
|
||||
/// <returns></returns>
|
||||
Task SetLocaleResources(LocaleResource localeResource);
|
||||
|
||||
/// <summary>
|
||||
/// 删除文化本地化资源信息
|
||||
/// </summary>
|
||||
/// <param name="localeResource"></param>
|
||||
/// <returns></returns>
|
||||
Task DeleteLocaleResources(LocaleResource localeResource);
|
||||
|
||||
/// <summary>
|
||||
/// 重载本地化资源
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
Task ReloadLocaleResources(string culture);
|
||||
|
||||
/// <summary>
|
||||
/// 移除文化本地化资源缓存
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
Task RemoveLocaleResources(string culture);
|
||||
|
||||
/// <summary>
|
||||
/// 指定文化本地化资源获和名城获取译文
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> GetLocaleResourcesString(string culture, string name);
|
||||
}
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过文化名城获取对应的本地化译文,如果缓存不存在则从数据库加载
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Dictionary<string, string>> GetLocaleResourcesAsync(string culture)
|
||||
{
|
||||
var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
|
||||
var cacheData = GetCache<Dictionary<string, string>>(key);
|
||||
if (cacheData == null)
|
||||
{
|
||||
var language = await GetLanguageByCulture(culture);
|
||||
if (language == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var data = await _dbContext.LocaleResources.Where(p => p.LanguageId == language.Id).ToDictionaryAsync(p => p.Name, p => p.Value);
|
||||
if (data.Any())
|
||||
{
|
||||
cacheData = data;
|
||||
SetCache(key, cacheData);
|
||||
}
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加或更新文化本地化信息
|
||||
/// </summary>
|
||||
/// <param name="localeResource"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SetLocaleResources(LocaleResource localeResource)
|
||||
{
|
||||
var language = await GetLanguageById(localeResource.LanguageId);
|
||||
if (language == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var key = $"{CacheKeys.LocaleResourcePrefix}{language.Culture}";
|
||||
var data = await GetLocaleResourcesAsync(language.Culture);
|
||||
if (data.ContainsKey(localeResource.Name))
|
||||
{
|
||||
data[localeResource.Name] = localeResource.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.Add(localeResource.Name, localeResource.Value);
|
||||
}
|
||||
SetCache(key, data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除文化本地化资源信息
|
||||
/// </summary>
|
||||
/// <param name="localeResource"></param>
|
||||
/// <returns></returns>
|
||||
public async Task DeleteLocaleResources(LocaleResource localeResource)
|
||||
{
|
||||
var language = await GetLanguageById(localeResource.LanguageId);
|
||||
if (language == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var key = $"{CacheKeys.LocaleResourcePrefix}{language.Culture}";
|
||||
var data = await GetLocaleResourcesAsync(language.Culture);
|
||||
data.Remove(localeResource.Name);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 重载本地化资源
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public async Task ReloadLocaleResources(string culture)
|
||||
{
|
||||
var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
|
||||
var cacheData = await GetCacheAsync<Dictionary<string, string>>(key);
|
||||
var language = await GetLanguageByCulture(culture);
|
||||
if (language == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var data = await _dbContext.LocaleResources.Where(p => p.LanguageId == language.Id).ToDictionaryAsync(p => p.Name, p => p.Value);
|
||||
|
||||
if (data.Any())
|
||||
{
|
||||
cacheData = data;
|
||||
await SetCacheAsync(key, cacheData);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除文化本地化资源缓存
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <returns></returns>
|
||||
public async Task RemoveLocaleResources(string culture)
|
||||
{
|
||||
var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
|
||||
await Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指定文化本地化资源获和名城获取译文
|
||||
/// </summary>
|
||||
/// <param name="culture"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> GetLocaleResourcesString(string culture, string name)
|
||||
{
|
||||
var key = $"{CacheKeys.LocaleResourcePrefix}{culture}";
|
||||
var cacheData = await GetCacheAsync<Dictionary<string, string>>(key);
|
||||
if (cacheData == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
if (cacheData.TryGetValue(name, out string value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Atomx.Data/CacheServices/LocalizationCacheService.cs
Normal file
30
Atomx.Data/CacheServices/LocalizationCacheService.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// Task SetLocalization(string id, LocalizationModel model);
|
||||
|
||||
// LocalizationModel GetLocalization(string id);
|
||||
// }
|
||||
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// public async Task SetLocalization(string id, LocalizationModel model)
|
||||
// {
|
||||
// var key = $"{CacheKeys.LocalizationPrefix}{id}";
|
||||
// await SetCacheAsync(key, model);
|
||||
// }
|
||||
|
||||
// public LocalizationModel GetLocalization(string id)
|
||||
// {
|
||||
// var key = $"{CacheKeys.LocalizationPrefix}{id}";
|
||||
// var cacheData = GetCache<LocalizationModel>(key);
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new LocalizationModel();
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
// }
|
||||
|
||||
//}
|
||||
69
Atomx.Data/CacheServices/LocalizedPropertyCacheService.cs
Normal file
69
Atomx.Data/CacheServices/LocalizedPropertyCacheService.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
//using Atomx.Common.Entities;
|
||||
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 通过数据对象实体ID和语言,获取或更新本地化数据
|
||||
// /// </summary>
|
||||
// /// <param name="entityId"></param>
|
||||
// /// <param name="language"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<LocalizedProperty>> GetLocalizedProperty(long entityId, int language, List<LocalizedProperty>? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过数据对象实体ID获取本地化数据
|
||||
// /// </summary>
|
||||
// /// <param name="entityId"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<LocalizedProperty>> GetLocalizedPropertyList(long entityId);
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 通过数据对象实体ID和语言,获取或更新本地化数据
|
||||
// /// </summary>
|
||||
// /// <param name="entityId"></param>
|
||||
// /// <param name="language"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<LocalizedProperty>> GetLocalizedProperty(long entityId, int language, List<LocalizedProperty>? data = null)
|
||||
// {
|
||||
// var cacheData = await GetLocalizedPropertyList(entityId);
|
||||
// if (data != null)
|
||||
// {
|
||||
// var list = cacheData.Where(p => p.LanguageId == language).ToList();
|
||||
// if (list.Any())
|
||||
// {
|
||||
// cacheData.RemoveAll(p => p.LanguageId == language);
|
||||
// }
|
||||
// cacheData.AddRange(data);
|
||||
// await SetCacheAsync($"{CacheKeys.LocalizedPropertyPrefix}{entityId}", cacheData);
|
||||
// return data;
|
||||
// }
|
||||
// return cacheData.Where(p => p.LanguageId == language).ToList();
|
||||
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过数据对象实体ID获取本地化数据
|
||||
// /// </summary>
|
||||
// /// <param name="entityId"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<LocalizedProperty>> GetLocalizedPropertyList(long entityId)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<LocalizedProperty>>($"{CacheKeys.LocalizedPropertyPrefix}{entityId}");
|
||||
// if(cacheData == null)
|
||||
// {
|
||||
// cacheData = _dbContext.LocalizedProperties.Where(p=>p.EntityId == entityId).ToList();
|
||||
// if(cacheData == null)
|
||||
// {
|
||||
// await SetCacheAsync($"{CacheKeys.LocalizedPropertyPrefix}{entityId}", cacheData);
|
||||
// }
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
107
Atomx.Data/CacheServices/ManufacturerCacheService.cs
Normal file
107
Atomx.Data/CacheServices/ManufacturerCacheService.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
//using Atomx.Common.Entities;
|
||||
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取所有可用的制造商品牌信息
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Manufacturer>> GetManufacturers();
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取或更新缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<Manufacturer> GetManufacturerById(long id, Manufacturer? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过名字查询
|
||||
// /// </summary>
|
||||
// /// <param name="name"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<Manufacturer> GetManufacturerByName(string name);
|
||||
|
||||
// /// <summary>
|
||||
// /// 从缓存中删除数据
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemoveManufacturer(long id);
|
||||
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// public async Task<Manufacturer> GetManufacturerById(long id, Manufacturer? data = null)
|
||||
// {
|
||||
// var cacheData = await GetManufacturers();
|
||||
// if (data != null)
|
||||
// {
|
||||
// var manufacturer = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (manufacturer != null)
|
||||
// {
|
||||
// cacheData.Remove(manufacturer);
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.Manufacturers, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.Manufacturers, cacheData);
|
||||
// }
|
||||
// return data;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var manufacturer = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (manufacturer != null)
|
||||
// {
|
||||
// return manufacturer;
|
||||
// }
|
||||
// return new Manufacturer();
|
||||
// }
|
||||
// }
|
||||
|
||||
// public async Task<Manufacturer> GetManufacturerByName(string name)
|
||||
// {
|
||||
// var cacheData = await GetManufacturers();
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// var data = cacheData.SingleOrDefault(p => p.Name == name);
|
||||
// if (data != null)
|
||||
// {
|
||||
// return data;
|
||||
// }
|
||||
// }
|
||||
// return new Manufacturer();
|
||||
// }
|
||||
|
||||
// public async Task<List<Manufacturer>> GetManufacturers()
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<Manufacturer>>(CacheKeys.Manufacturers);
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new List<Manufacturer>();
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task RemoveManufacturer(long id)
|
||||
// {
|
||||
// var cacheData = await GetManufacturers();
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData.Remove(data);
|
||||
// await SetCacheAsync(CacheKeys.Manufacturers, cacheData);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
89
Atomx.Data/CacheServices/PaymentChannelsCacheService.cs
Normal file
89
Atomx.Data/CacheServices/PaymentChannelsCacheService.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取所有可用的支付通道
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<List<PaymentChannel>> GetPaymentChannels(List<PaymentChannel>? languages = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取或更新缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<PaymentChannel> GetPaymentChannelById(long id, PaymentChannel? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID 删除
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemovePaymentChannelById(long id);
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// public async Task<PaymentChannel> GetPaymentChannelById(long id, PaymentChannel? data = null)
|
||||
// {
|
||||
// var cacheData = await GetPaymentChannels();
|
||||
// if (data != null)
|
||||
// {
|
||||
// var language = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (language != null)
|
||||
// {
|
||||
// cacheData.Remove(language);
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.PaymentChannels, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.PaymentChannels, cacheData);
|
||||
// }
|
||||
// return data;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var manufacturer = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (manufacturer != null)
|
||||
// {
|
||||
// return manufacturer;
|
||||
// }
|
||||
// return new PaymentChannel();
|
||||
// }
|
||||
// }
|
||||
|
||||
// public async Task<List<PaymentChannel>> GetPaymentChannels(List<PaymentChannel>? languages = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<PaymentChannel>>(CacheKeys.PaymentChannels);
|
||||
// if (languages != null)
|
||||
// {
|
||||
// cacheData = languages;
|
||||
// await SetCacheAsync(CacheKeys.PaymentChannels, cacheData);
|
||||
// }
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new List<PaymentChannel>();
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task RemovePaymentChannelById(long id)
|
||||
// {
|
||||
// var cacheData = await GetPaymentChannels();
|
||||
// if (cacheData != null)
|
||||
// {
|
||||
// var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData.Remove(data);
|
||||
// await SetCacheAsync(CacheKeys.PaymentChannels, cacheData);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
16
Atomx.Data/CacheServices/ProductAttributeCacheService.cs
Normal file
16
Atomx.Data/CacheServices/ProductAttributeCacheService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
|
||||
}
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
}
|
||||
}
|
||||
401
Atomx.Data/CacheServices/ProductCacheService.cs
Normal file
401
Atomx.Data/CacheServices/ProductCacheService.cs
Normal file
@@ -0,0 +1,401 @@
|
||||
//using Atomx.Common.Entities;
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取每日热销产品数据
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Product>> GetDailyBestSells(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取产品详情
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<ProductDetailCachedModel?> GetProductDetails(long id, bool reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取营销产品
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<MarketingProductModel> GetMarketingProducts(long id, MarketingProductModel? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 删除活动产品
|
||||
// /// </summary>
|
||||
// /// <param name="productId"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemoveMarketingProductByProductId(long marketingId, long productId);
|
||||
|
||||
// /// <summary>
|
||||
// /// 删除指定营销活动产品缓存
|
||||
// /// </summary>
|
||||
// /// <param name="marketingId"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemoveMarketingProductByMarketingId(long marketingId);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取营销产品
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<List<MarketingProductModel>> GetMarketingProducts();
|
||||
|
||||
// /// <summary>
|
||||
// /// 删除产品缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// Task RemoveProduct(long id);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取最新产品
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Product>> GetNewProducts(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取分类下的最新产品
|
||||
// /// </summary>
|
||||
// /// <param name="categoryId"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Product>> GetNewProductsByCategory(List<long> categoryIds, bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取热销产品
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Product>> GetHotProducts(bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取分类下热销产品
|
||||
// /// </summary>
|
||||
// /// <param name="categoryId"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Product>> GetHotProductsByCategory(List<long> categoryIds, bool? reload = false);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取首页推荐得精选产品
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<Product>> GetIndexFeatured(bool? reload = false);
|
||||
// }
|
||||
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取每日热销产品数据
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Product>> GetDailyBestSells(bool? reload = false)
|
||||
// {
|
||||
// var key = $"{CacheKeys.DailyBestSellProductPrefix}{DateTime.UtcNow.Day}";
|
||||
// var cacheData = await GetCacheAsync<List<Product>>(key);
|
||||
// var needReload = reload.HasValue ? reload.Value : false;
|
||||
// if (cacheData == null || needReload)
|
||||
// {
|
||||
// var query = from p in _dbContext.Products
|
||||
// where p.Status == (int)ProductStatus.Publish && p.StoreId == 0
|
||||
// select p;
|
||||
// cacheData = query.OrderByDescending(p => EF.Functions.Random()).Take(100).ToList();
|
||||
// await SetCacheAsync(key, cacheData);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// public async Task<ProductDetailCachedModel?> GetProductDetails(long id, bool reload = false)
|
||||
// {
|
||||
// if (id == 0)
|
||||
// {
|
||||
// return null;
|
||||
// }
|
||||
// var productData = await GetCacheAsync<ProductDetailCachedModel>($"{CacheKeys.ProductPrefix}{id}");
|
||||
|
||||
// if (productData == null || reload)
|
||||
// {
|
||||
// productData = (from p in _dbContext.Products
|
||||
// where p.Id == id && p.Status == (int)ProductStatus.Publish
|
||||
// select new ProductDetailCachedModel
|
||||
// {
|
||||
// AllowExchange = p.AllowExchange,
|
||||
// AllowReturn = p.AllowReturn,
|
||||
// AvailableEndTime = p.AvailableEndTime,
|
||||
// AvailableStartTime = p.AvailableStartTime,
|
||||
// Body = p.Body,
|
||||
// CategoryId = p.CategoryId,
|
||||
// CategoryPath = p.CategoryPath,
|
||||
// CreateTime = p.CreateTime,
|
||||
// Deleted = p.Deleted,
|
||||
// Description = p.Description,
|
||||
// DisableCoupons = p.DisableCoupons,
|
||||
// DisplayOrder = p.DisplayOrder,
|
||||
// Feature = p.Feature,
|
||||
// IsFeatured = p.IsFeatured,
|
||||
// Id = p.Id,
|
||||
// Image = p.Image,
|
||||
// InHouse = p.InHouse,
|
||||
// IsWarehouse = p.IsWarehouse,
|
||||
// Localized = new(),
|
||||
// ManufacturerId = p.ManufacturerId,
|
||||
// MarketPrice = p.MarketPrice,
|
||||
// MetaDescription = p.MetaDescription,
|
||||
// MetaTitle = p.MetaTitle,
|
||||
// Photos = p.Photos,
|
||||
// PictureCount = p.PictureCount,
|
||||
// Price = p.Price,
|
||||
// ProductTypeId = p.ProductTypeId,
|
||||
// Remarks = p.Remarks,
|
||||
// ReviewStatus = p.ReviewStatus,
|
||||
// SaleAttribute = new(),
|
||||
// SalesQuantity = p.SalesQuantity,
|
||||
// ShippingId = p.ShippingId,
|
||||
// SingleShip = p.SingleShip,
|
||||
// Sku = p.Sku,
|
||||
// SKUCombinations = new(),
|
||||
// SkuPrices = p.SkuPrices,
|
||||
// Slug = p.Slug,
|
||||
// Specification = p.Specification,
|
||||
// Status = p.Status,
|
||||
// StockQuantity = p.StockQuantity,
|
||||
// StopSales = p.StopSales,
|
||||
// StoreId = p.StoreId,
|
||||
// Tags = p.Tags,
|
||||
// Title = p.Title,
|
||||
// UpdateTime = p.UpdateTime,
|
||||
// UserId = p.UserId,
|
||||
// VendorId = p.VendorId,
|
||||
// ViewsCount = p.ViewsCount,
|
||||
// WarehouseId = p.WarehouseId,
|
||||
// Weight = p.Weight,
|
||||
// FiveStars = p.FiveStars,
|
||||
// FourStars = p.FourStars,
|
||||
// IsBest = p.IsBest,
|
||||
// IsNew = p.IsNew,
|
||||
// OneStars = p.OneStars,
|
||||
// OrderMaximumQuantity = p.OrderMaximumQuantity,
|
||||
// OrderMinimumQuantity = p.OrderMinimumQuantity,
|
||||
// Ratings = p.Ratings,
|
||||
// ReviewsCount = p.ReviewsCount,
|
||||
// ShippingDays = p.ShippingDays,
|
||||
// ThreeStars = p.ThreeStars,
|
||||
// TwoStars = p.TwoStars
|
||||
// }).SingleOrDefault();
|
||||
|
||||
// if (productData == null)
|
||||
// {
|
||||
// return null;
|
||||
// }
|
||||
// var productSaleAttributes = (from p in _dbContext.ProductAttributeRelations
|
||||
// join sa in _dbContext.ProductAttributes on p.AttributeId equals sa.Id into table
|
||||
// from a in table.DefaultIfEmpty()
|
||||
// where p.ProductId == id && a.IsSpecification == false
|
||||
// orderby p.DisplayOrder descending
|
||||
// select new ProductSKUAttributeModel
|
||||
// {
|
||||
// AttributeId = p.AttributeId,
|
||||
// AttributeName = a.Name,
|
||||
// Items = new(),
|
||||
// ProductAttributeId = p.Id
|
||||
// }).ToList();
|
||||
// if (productSaleAttributes.Any())
|
||||
// {
|
||||
// var productAttributeIds = productSaleAttributes.Select(p => p.ProductAttributeId).ToList();
|
||||
// var attributeValues = (from p in _dbContext.ProductAttributeValues
|
||||
// where productAttributeIds.Contains(p.ProductAttributeRelationId)
|
||||
// orderby p.DisplayOrder descending
|
||||
// select new ProductSKUAttributeValueModel
|
||||
// {
|
||||
// Id = p.OptionId,
|
||||
// Name = p.Name,
|
||||
// ProductAttributeId = p.ProductAttributeRelationId
|
||||
// }
|
||||
// ).ToList();
|
||||
// foreach (var item in productSaleAttributes)
|
||||
// {
|
||||
// var values = attributeValues.Where(p => p.ProductAttributeId == item.ProductAttributeId).ToList();
|
||||
// item.Items = values;
|
||||
// }
|
||||
|
||||
// productData.SaleAttribute = productSaleAttributes;
|
||||
// var skuCombinations = _dbContext.ProductSKUCombinations.Where(p => p.ProductId == id).ToList();
|
||||
// productData.SKUCombinations = skuCombinations;
|
||||
// }
|
||||
// await SetCacheAsync($"{CacheKeys.ProductPrefix}{id}", productData);
|
||||
// }
|
||||
|
||||
// return productData;
|
||||
// }
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取营销产品
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<MarketingProductModel> GetMarketingProducts(long id, MarketingProductModel? data = null)
|
||||
// {
|
||||
// var cacheData = await GetMarketingProducts();
|
||||
// if (data != null)
|
||||
// {
|
||||
// cacheData.RemoveAll(p => p.Id == id);
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.MarketingProduct, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// }
|
||||
// return data;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 删除活动产品
|
||||
// /// </summary>
|
||||
// /// <param name="productId"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task RemoveMarketingProductByProductId(long marketingId, long productId)
|
||||
// {
|
||||
// var cacheData = await GetMarketingProducts();
|
||||
// cacheData.RemoveAll(p => p.ProductId == productId && p.MarketingId == marketingId);
|
||||
|
||||
// await SetCacheAsync(CacheKeys.MarketingProduct, cacheData);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 删除指定营销活动产品缓存
|
||||
// /// </summary>
|
||||
// /// <param name="marketingId"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task RemoveMarketingProductByMarketingId(long marketingId)
|
||||
// {
|
||||
// var cacheData = await GetMarketingProducts();
|
||||
// cacheData.RemoveAll(p => p.MarketingId == marketingId);
|
||||
|
||||
// await SetCacheAsync(CacheKeys.MarketingProduct, cacheData);
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取营销产品
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<MarketingProductModel>> GetMarketingProducts()
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<MarketingProductModel>>(CacheKeys.MarketingProduct);
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// return new List<MarketingProductModel>();
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task RemoveProduct(long id)
|
||||
// {
|
||||
// var productData = await GetCacheAsync<ProductDetailCachedModel>($"{CacheKeys.ProductPrefix}{id}");
|
||||
// if (productData == null)
|
||||
// {
|
||||
// await Remove($"{CacheKeys.ProductPrefix}{id}");
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取最新产品
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Product>> GetNewProducts(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<Product>>(CacheKeys.ProductNewList);
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.Products.OrderByDescending(p => p.CreateTime).Take(100).ToList();
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync(CacheKeys.MarketingProduct, cacheData, 24);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取分类下的最新产品
|
||||
// /// </summary>
|
||||
// /// <param name="categoryId"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Product>> GetNewProductsByCategory(List<long> categoryIds, bool? reload = false)
|
||||
// {
|
||||
// var key = string.Join(",", categoryIds.ToArray()).ToMD5();
|
||||
// var cacheData = await GetCacheAsync<List<Product>>($"{CacheKeys.ProductNewListPrefix}.{key}");
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.Products.Where(p => categoryIds.Contains(p.CategoryId)).OrderByDescending(p => p.CreateTime).Take(100).ToList();
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync($"{CacheKeys.ProductNewListPrefix}.{key}", cacheData, 24);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取热销产品
|
||||
// /// </summary>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Product>> GetHotProducts(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<Product>>(CacheKeys.ProductHotList);
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.Products.OrderByDescending(p => p.SalesQuantity).Take(100).ToList();
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync(CacheKeys.ProductHotList, cacheData, 24);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取分类下热销产品
|
||||
// /// </summary>
|
||||
// /// <param name="categoryId"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Product>> GetHotProductsByCategory(List<long> categoryIds, bool? reload = false)
|
||||
// {
|
||||
// var key = string.Join(",", categoryIds.ToArray()).ToMD5();
|
||||
// var cacheData = await GetCacheAsync<List<Product>>($"{CacheKeys.ProductHotListPrefix}.{key}");
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.Products.Where(p => categoryIds.Contains(p.CategoryId)).OrderByDescending(p => p.SalesQuantity).Take(100).ToList();
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync($"{CacheKeys.ProductNewListPrefix}.{key}", cacheData, 24);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取首页推荐得精选产品
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<Product>> GetIndexFeatured(bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<Product>>(CacheKeys.ProductFeaturedList);
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.Products.Where(p => p.IsFeatured).OrderByDescending(p => p.UpdateTime).Take(20).ToList();
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync(CacheKeys.ProductFeaturedList, cacheData, 24);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
110
Atomx.Data/CacheServices/RoleCacheService.cs
Normal file
110
Atomx.Data/CacheServices/RoleCacheService.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 重新加载角色
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task ReloadRoleAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Role>> GetRolesAsync(bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取角色
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<Role?> GetRoleById(long roleId, bool? reload = false);
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的权限点
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<Permission>> GetAllPermissions(bool? reload = false);
|
||||
}
|
||||
|
||||
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 重新加载角色
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task ReloadRoleAsync()
|
||||
{
|
||||
|
||||
var data = _dbContext.Roles.Where(p => p.Enabled).ToList();
|
||||
if (data.Any())
|
||||
{
|
||||
await SetCacheAsync(CacheKeys.Roles, data);
|
||||
}
|
||||
}
|
||||
|
||||
// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Role>> GetRolesAsync(bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
var cacheData = await GetCacheAsync<List<Role>>(CacheKeys.Roles);
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var roles = (from p in _dbContext.Roles
|
||||
where p.Enabled
|
||||
select p).ToList();
|
||||
await SetCacheAsync(CacheKeys.Roles, roles);
|
||||
return roles;
|
||||
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过ID获取角色
|
||||
/// </summary>
|
||||
/// <param name="roleId"></param>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Role?> GetRoleById(long roleId, bool? reload = false)
|
||||
{
|
||||
var cacheData = await GetRolesAsync(reload);
|
||||
return cacheData.Where(p => p.Id == roleId).SingleOrDefault();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的权限点
|
||||
/// </summary>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<Permission>> GetAllPermissions(bool? reload = false)
|
||||
{
|
||||
bool reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
var cacheData = await GetCacheAsync<List<Permission>>(CacheKeys.Permissions);
|
||||
if (cacheData == null || reloadData)
|
||||
{
|
||||
var permissions = await (from p in _dbContext.Permissions
|
||||
select p).ToListAsync();
|
||||
await SetCacheAsync(CacheKeys.Permissions, permissions);
|
||||
return permissions;
|
||||
|
||||
}
|
||||
return cacheData;
|
||||
}
|
||||
}
|
||||
}
|
||||
66
Atomx.Data/CacheServices/StoreCacheService.cs
Normal file
66
Atomx.Data/CacheServices/StoreCacheService.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
//using Atomx.Common.Entities;
|
||||
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过用户ID获取店铺信息
|
||||
// /// </summary>
|
||||
// /// <param name="uid"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<Store?> GetStore(long id);
|
||||
|
||||
// /// <summary>
|
||||
// /// 获取店铺信息
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="store"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<Store> GetStore(long id, Store store);
|
||||
|
||||
// /// <summary>
|
||||
// /// 重载店铺信息
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// Task ReloadStore(long id);
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 通过用户ID获取店铺信息
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<Store?> GetStore(long id)
|
||||
// {
|
||||
// string key = $"{CacheKeys.StorePrefix}.{id}";
|
||||
// var cacheData = await GetCacheAsync<Store>(key);
|
||||
// if (cacheData == null)
|
||||
// {
|
||||
// var store = _dbContext.Stores.SingleOrDefault(p => p.Id == id);
|
||||
// cacheData = store;
|
||||
// await SetCacheAsync(key, cacheData);
|
||||
// }
|
||||
// return cacheData;
|
||||
// }
|
||||
|
||||
// public async Task<Store> GetStore(long id, Store store)
|
||||
// {
|
||||
// string key = $"{CacheKeys.StorePrefix}.{id}";
|
||||
// var cacheData = await GetCacheAsync<Store>(key);
|
||||
// await SetCacheAsync(key, store);
|
||||
// return store;
|
||||
// }
|
||||
|
||||
|
||||
// public async Task ReloadStore(long id)
|
||||
// {
|
||||
// var store = _dbContext.Stores.FirstOrDefault(p => p.Id == id);
|
||||
// string key = $"{CacheKeys.StorePrefix}.{id}";
|
||||
// await SetCacheAsync(key, store);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
101
Atomx.Data/CacheServices/StoreLevelCacheService.cs
Normal file
101
Atomx.Data/CacheServices/StoreLevelCacheService.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
|
||||
//namespace Atomx.Data.CacheServices
|
||||
//{
|
||||
// public partial interface ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取店铺等级列表
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<List<StoreLevel>> GetStoreLevels(bool? reload = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取或更新缓存
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="data"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<StoreLevel> GetStoreLevelById(long id, StoreLevel? data = null);
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取店铺登记
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// Task<StoreLevel> GetStoreLevel(long id, bool? reload = false);
|
||||
// }
|
||||
// public partial class CacheService : ICacheService
|
||||
// {
|
||||
// /// <summary>
|
||||
// /// 获取店铺等级列表
|
||||
// /// </summary>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<List<StoreLevel>> GetStoreLevels(bool? reload = null)
|
||||
// {
|
||||
// var cacheData = await GetCacheAsync<List<StoreLevel>>(CacheKeys.StoreLevel);
|
||||
// if (cacheData == null || reload.HasValue)
|
||||
// {
|
||||
// var data = _dbContext.StoreLevels.Where(p => p.Enabled).ToList();
|
||||
// if (data.Any())
|
||||
// {
|
||||
// cacheData = data;
|
||||
// await SetCacheAsync(CacheKeys.StoreLevel, cacheData);
|
||||
// }
|
||||
// }
|
||||
|
||||
// return cacheData;
|
||||
// }
|
||||
// public async Task<StoreLevel> GetStoreLevelById(long id, StoreLevel? data = null)
|
||||
// {
|
||||
// var cacheData = await GetStoreLevels();
|
||||
// if (cacheData == null || data != null)
|
||||
// {
|
||||
// var storeLevel = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (storeLevel != null)
|
||||
// {
|
||||
// cacheData.Remove(storeLevel);
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.StoreLevel, cacheData);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// cacheData.Add(data);
|
||||
// await SetCacheAsync(CacheKeys.StoreLevel, cacheData);
|
||||
// }
|
||||
// }
|
||||
// var level = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// return level;
|
||||
// }
|
||||
|
||||
// /// <summary>
|
||||
// /// 通过ID获取店铺登记
|
||||
// /// </summary>
|
||||
// /// <param name="id"></param>
|
||||
// /// <param name="reload"></param>
|
||||
// /// <returns></returns>
|
||||
// public async Task<StoreLevel> GetStoreLevel(long id, bool? reload = false)
|
||||
// {
|
||||
// var cacheData = await GetStoreLevels();
|
||||
// var reloadData = reload.HasValue ? reload.Value : false;
|
||||
|
||||
// if (cacheData == null || reload == true)
|
||||
// {
|
||||
// var storeLevel = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// if (storeLevel != null)
|
||||
// {
|
||||
// cacheData.Remove(storeLevel);
|
||||
// }
|
||||
|
||||
// var level = _dbContext.StoreLevels.SingleOrDefault(p => p.Id == id);
|
||||
// cacheData.Add(level);
|
||||
// await SetCacheAsync(CacheKeys.StoreLevel, cacheData);
|
||||
// }
|
||||
// var data = cacheData.SingleOrDefault(p => p.Id == id);
|
||||
// return data;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
57
Atomx.Data/CacheServices/UserCacheService.cs
Normal file
57
Atomx.Data/CacheServices/UserCacheService.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Atomx.Common.Constant;
|
||||
using Atomx.Common.Entities;
|
||||
|
||||
namespace Atomx.Data.CacheServices
|
||||
{
|
||||
public partial interface ICacheService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取用户基本信息
|
||||
/// </summary>
|
||||
/// <param name="agencyId"></param>
|
||||
/// <param name="reload"></param>
|
||||
/// <returns></returns>
|
||||
Task<User?> GetUserBaseInfo(long id, bool? reload = null);
|
||||
|
||||
/// <summary>
|
||||
/// 更新用户缓存
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
Task<User> UpdateUserBaseInfo(User user);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public partial class CacheService : ICacheService
|
||||
{
|
||||
public async Task<User?> GetUserBaseInfo(long id, bool? reload = null)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<User>($"{CacheKeys.UserPrefix}{id}");
|
||||
|
||||
if (cacheData == null || reload.HasValue)
|
||||
{
|
||||
var data = _dbContext.Users.Where(p => p.Id == id).SingleOrDefault();
|
||||
if (data != null)
|
||||
{
|
||||
cacheData = data;
|
||||
await SetCacheAsync($"{CacheKeys.UserPrefix}{id}", cacheData, 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
|
||||
public async Task<User> UpdateUserBaseInfo(User user)
|
||||
{
|
||||
var cacheData = await GetCacheAsync<User>($"{CacheKeys.UserPrefix}{user.Id}");
|
||||
cacheData = user;
|
||||
await SetCacheAsync($"{CacheKeys.UserPrefix}{user.Id}", cacheData, 0.5);
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user