From d16a678b8b08f301142b572f43cecaebf91a24b8 Mon Sep 17 00:00:00 2001 From: zpc Date: Mon, 9 Dec 2024 17:10:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AppController.cs | 36 +- .../Cache/CloudGamingCache.cs | 514 +++++++++--------- .../Cache/Special/ImageEntityCache.cs | 371 +++++++------ 3 files changed, 455 insertions(+), 466 deletions(-) diff --git a/src/CloudGaming/Api/CloudGaming.Api/Controllers/AppController.cs b/src/CloudGaming/Api/CloudGaming.Api/Controllers/AppController.cs index 6d87923..3c30d44 100644 --- a/src/CloudGaming/Api/CloudGaming.Api/Controllers/AppController.cs +++ b/src/CloudGaming/Api/CloudGaming.Api/Controllers/AppController.cs @@ -5,31 +5,27 @@ using CloudGaming.DtoModel; using HuanMeng.DotNetCore.AttributeExtend; using Microsoft.AspNetCore.Mvc; +namespace CloudGaming.Api.Controllers; -// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 - -namespace CloudGaming.Api.Controllers +/// +/// +/// +public class AppController : CloudGamingControllerBase { + public AppController(IServiceProvider _serviceProvider) : base(_serviceProvider) + { + } /// /// /// - public class AppController : CloudGamingControllerBase + /// + [HttpGet] + public async Task GetAppConfigAsync() { - public AppController(IServiceProvider _serviceProvider) : base(_serviceProvider) - { - } - /// - /// - /// - /// - [HttpGet] - public async Task GetAppConfigAsync() - { - AppConfigBLL appConfigBLL = new AppConfigBLL(ServiceProvider); - var appConfig = await appConfigBLL.GetAppConfig(); - return appConfig; - } - - + AppConfigBLL appConfigBLL = new AppConfigBLL(ServiceProvider); + var appConfig = await appConfigBLL.GetAppConfig(); + return appConfig; } + + } diff --git a/src/CloudGaming/Code/CloudGaming.Code/Cache/CloudGamingCache.cs b/src/CloudGaming/Code/CloudGaming.Code/Cache/CloudGamingCache.cs index b8b6522..af6c39b 100644 --- a/src/CloudGaming/Code/CloudGaming.Code/Cache/CloudGamingCache.cs +++ b/src/CloudGaming/Code/CloudGaming.Code/Cache/CloudGamingCache.cs @@ -12,318 +12,314 @@ using HuanMeng.DotNetCore.CacheHelper; using System.Collections.Concurrent; using System.Linq.Expressions; -namespace CloudGaming.Code.Cache +namespace CloudGaming.Code.Cache; + +/// +/// 缓存数据表 +/// +public class CloudGamingCache { - /// - /// 缓存数据表 - /// - public class CloudGamingCache + private readonly CloudGamingBase _gamingBase; + + public CloudGamingCache(CloudGamingBase gamingBase) { - private readonly CloudGamingBase _gamingBase; + _gamingBase = gamingBase; + } - public CloudGamingCache(CloudGamingBase gamingBase) + #region 通用缓存获取方法 + + /// + /// 通用缓存获取方法,适用于所有类型 + /// + private List GetCacheList(ref CommonDataEntityCache? cache, Expression>? expWhere = null) where T : class + { + if (cache == null) { - _gamingBase = gamingBase; + cache = CloudGamingCacheExtend.GetDataEntityCache(_gamingBase, expWhere); } + return cache.DataList ?? new List(); + } - #region 通用缓存获取方法 + #endregion - /// - /// 通用缓存获取方法,适用于所有类型 - /// - private List GetCacheList(ref CommonDataEntityCache? cache, Expression>? expWhere = null) where T : class + #region 图片缓存表 + + private CommonDataEntityCache? _appImageCache; + + /// + /// 图片缓存列表 + /// + public List AppImageList => GetCacheList(ref _appImageCache); + + #endregion + + #region 配置缓存表 + + private CommonDataEntityCache? _appConfigCache; + + /// + /// 配置缓存列表 + /// + public List AppConfigList + { + get { - if (cache == null) + if (_appConfigCache == null) { - cache = CloudGamingCacheExtend.GetDataEntityCache(_gamingBase, expWhere); + _appConfigCache = new AppConfigEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper, _gamingBase.AppConfig); } - return cache.DataList ?? new List(); + return _appConfigCache.DataList ?? new List(); } + } - #endregion - #region 图片缓存表 + #endregion - private CommonDataEntityCache? _appImageCache; + #region 首页缓存表 - /// - /// 图片缓存列表 - /// - public List AppImageList => GetCacheList(ref _appImageCache); + private CommonDataEntityCache? _epgCategoryCfg; - #endregion + /// + /// 菜单分类 + /// + public List EpgCategoryCfg => GetCacheList(ref _epgCategoryCfg, it => it.IsOnline); + #endregion - #region 配置缓存表 - private CommonDataEntityCache? _appConfigCache; + private GameEntityCache? gameEntityCache; - /// - /// 配置缓存列表 - /// - public List AppConfigList + /// + /// 游戏缓存 + /// + public GameEntityCache GameEntityCache + { + get { - get + if (gameEntityCache == null) { - if (_appConfigCache == null) - { - _appConfigCache = new AppConfigEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper, _gamingBase.AppConfig); - } - return _appConfigCache.DataList ?? new List(); + gameEntityCache = new GameEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper, _gamingBase.AppConfig); } + return gameEntityCache; } - - - #endregion - - #region 首页缓存表 - - private CommonDataEntityCache? _epgCategoryCfg; - - /// - /// 菜单分类 - /// - public List EpgCategoryCfg => GetCacheList(ref _epgCategoryCfg, it => it.IsOnline); - #endregion - - - private GameEntityCache? gameEntityCache; - - /// - /// 游戏缓存 - /// - public GameEntityCache GameEntityCache + } + /// + /// 游戏列表 + /// + public List GameInfos + { + get { - get - { - if (gameEntityCache == null) - { - gameEntityCache = new GameEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper, _gamingBase.AppConfig); - } - return gameEntityCache; - } + + return GameEntityCache?.DataList ?? new List(); } - /// - /// 游戏列表 - /// - public List GameInfos + } + + #region 图片缓存 + + /// + /// + /// + private ImageEntityCache imageEntityCache; + + /// + /// 图片缓存 + /// + public ImageEntityCache ImageEntityCache + { + get { - get + if (imageEntityCache == null) { - - return GameEntityCache?.DataList ?? new List(); + imageEntityCache = new ImageEntityCache(_gamingBase.Dao, _gamingBase.AppConfig, _gamingBase.RedisCache, _gamingBase.AppRequestInfo); } + return imageEntityCache; } + } + #endregion - #region 图片缓存 + /// + /// + /// + private ProductCacheEntityCache productCacheEntityCache; - /// - /// - /// - private ImageEntityCache imageEntityCache; - - /// - /// 图片缓存 - /// - public ImageEntityCache ImageEntityCache + /// + /// 产品缓存 + /// + public ProductCacheEntityCache ProductCacheEntityCache + { + get { - get + if (productCacheEntityCache == null) { - if (imageEntityCache == null) - { - imageEntityCache = new ImageEntityCache(_gamingBase.Dao, _gamingBase.AppConfig, _gamingBase.RedisCache, _gamingBase.AppRequestInfo); - } - return imageEntityCache; + productCacheEntityCache = new ProductCacheEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper); } + return productCacheEntityCache; } - #endregion - - /// - /// - /// - private ProductCacheEntityCache productCacheEntityCache; - - /// - /// 产品缓存 - /// - public ProductCacheEntityCache ProductCacheEntityCache - { - get - { - if (productCacheEntityCache == null) - { - productCacheEntityCache = new ProductCacheEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper); - } - return productCacheEntityCache; - } - } - - /// - /// 产品列表 - /// - public List ProductCacheList - { - get - { - return ProductCacheEntityCache.DataList ?? new List(); - } - } - - #region 七天签到 - /// - /// - /// - private SevenDayEntityCache sevenDayEntityCache; - - /// - /// 七天签到缓存 - /// - public SevenDayEntityCache SevenDayEntityCache - { - get - { - if (sevenDayEntityCache == null) - { - sevenDayEntityCache = new SevenDayEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper); - } - return sevenDayEntityCache; - } - } - - /// - /// 七天签到列表 - /// - public List SevenDayList - { - get - { - return SevenDayEntityCache.DataList ?? new List(); - } - } - #endregion - - #region 兑换码 - /// - /// - /// - private RedemptionCodeEntityCache redemptionCodeEntityCache; - - /// - /// 兑换码缓存 - /// - public RedemptionCodeEntityCache RedemptionCodeEntityCache - { - get - { - if (redemptionCodeEntityCache == null) - { - redemptionCodeEntityCache = new RedemptionCodeEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper); - } - return redemptionCodeEntityCache; - } - } - - /// - /// 兑换码列表 - /// - public List RedemptionCodeList - { - get - { - return RedemptionCodeEntityCache.DataList ?? new List(); - } - } - #endregion - - - - - #region 首页缓存表 - - - - #endregion } /// - /// 游戏缓存扩展 + /// 产品列表 /// - public static class CloudGamingCacheExtend + public List ProductCacheList { - private static readonly ConcurrentDictionary ExtCacheLockList = new ConcurrentDictionary(); - private static readonly ConcurrentDictionary GameCacheLockList = new ConcurrentDictionary(); - private static readonly ConcurrentDictionary AppCacheLockList = new ConcurrentDictionary(); - private static readonly ConcurrentDictionary UserCacheLockList = new ConcurrentDictionary(); - - /// - /// 命名空间与缓存映射 - /// - private static readonly Dictionary cacheList, AppDataBaseType dbType)> NamespaceMapping; - - static CloudGamingCacheExtend() + get { - NamespaceMapping = new Dictionary cacheList, AppDataBaseType dbType)> - { - { "CloudGaming.GameModel.Db.Db_Ext", (ExtCacheLockList, AppDataBaseType.Ext) }, - { "CloudGaming.GameModel.Db.Db_Game", (GameCacheLockList, AppDataBaseType.Game) }, - { "CloudGaming.Model.DbSqlServer.Db_Phone", (AppCacheLockList, AppDataBaseType.App) }, - { "CloudGaming.Model.DbSqlServer.Db_User", (UserCacheLockList, AppDataBaseType.User) } - }; + return ProductCacheEntityCache.DataList ?? new List(); } + } - /// - /// 获取实体缓存 - /// - public static CommonDataEntityCache GetDataEntityCache(CloudGamingBase cloudGamingBase, Expression>? expWhere = null, int cacheTime = 36000) where T : class + #region 七天签到 + /// + /// + /// + private SevenDayEntityCache sevenDayEntityCache; + + /// + /// 七天签到缓存 + /// + public SevenDayEntityCache SevenDayEntityCache + { + get { - var typeLock = typeof(T); - var namespaceKey = typeLock.Namespace; - - if (namespaceKey == null || !NamespaceMapping.ContainsKey(namespaceKey)) + if (sevenDayEntityCache == null) { - throw new Exception($"缓存数据不存在或命名空间不匹配:{namespaceKey}"); + sevenDayEntityCache = new SevenDayEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper); } - - var (cacheList, dbType) = NamespaceMapping[namespaceKey]; - object cacheLock = GetOrAddCacheLock(typeLock, cacheList); - CacheBaseConfig cacheBaseConfig = cloudGamingBase.ToCacheBaseConfig(dbType); - - return new DataBaseEntityCache(cacheBaseConfig, cacheLock, expWhere: expWhere, cacheTime: cacheTime); + return sevenDayEntityCache; } + } - /// - /// 获取或添加缓存锁对象 - /// - private static object GetOrAddCacheLock(Type typeLock, ConcurrentDictionary cacheList) + /// + /// 七天签到列表 + /// + public List SevenDayList + { + get { - return cacheList.GetOrAdd(typeLock, _ => + return SevenDayEntityCache.DataList ?? new List(); + } + } + #endregion + + #region 兑换码 + /// + /// + /// + private RedemptionCodeEntityCache redemptionCodeEntityCache; + + /// + /// 兑换码缓存 + /// + public RedemptionCodeEntityCache RedemptionCodeEntityCache + { + get + { + if (redemptionCodeEntityCache == null) { - Log($"没有找到锁对象 ==> {typeLock.Name}"); - return new object(); - }); + redemptionCodeEntityCache = new RedemptionCodeEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper); + } + return redemptionCodeEntityCache; + } + } + + /// + /// 兑换码列表 + /// + public List RedemptionCodeList + { + get + { + return RedemptionCodeEntityCache.DataList ?? new List(); + } + } + #endregion + + #region 首页缓存表 + + + + #endregion +} + +/// +/// 游戏缓存扩展 +/// +public static class CloudGamingCacheExtend +{ + private static readonly ConcurrentDictionary ExtCacheLockList = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary GameCacheLockList = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary AppCacheLockList = new ConcurrentDictionary(); + private static readonly ConcurrentDictionary UserCacheLockList = new ConcurrentDictionary(); + + /// + /// 命名空间与缓存映射 + /// + private static readonly Dictionary cacheList, AppDataBaseType dbType)> NamespaceMapping; + + static CloudGamingCacheExtend() + { + NamespaceMapping = new Dictionary cacheList, AppDataBaseType dbType)> + { + { "CloudGaming.GameModel.Db.Db_Ext", (ExtCacheLockList, AppDataBaseType.Ext) }, + { "CloudGaming.GameModel.Db.Db_Game", (GameCacheLockList, AppDataBaseType.Game) }, + { "CloudGaming.Model.DbSqlServer.Db_Phone", (AppCacheLockList, AppDataBaseType.App) }, + { "CloudGaming.Model.DbSqlServer.Db_User", (UserCacheLockList, AppDataBaseType.User) } + }; + } + + /// + /// 获取实体缓存 + /// + public static CommonDataEntityCache GetDataEntityCache(CloudGamingBase cloudGamingBase, Expression>? expWhere = null, int cacheTime = 36000) where T : class + { + var typeLock = typeof(T); + var namespaceKey = typeLock.Namespace; + + if (namespaceKey == null || !NamespaceMapping.ContainsKey(namespaceKey)) + { + throw new Exception($"缓存数据不存在或命名空间不匹配:{namespaceKey}"); } - /// - /// 根据数据库类型生成缓存配置 - /// - public static CacheBaseConfig ToCacheBaseConfig(this CloudGamingBase cloudGamingBase, AppDataBaseType appDataBaseType) - { - return appDataBaseType switch - { - AppDataBaseType.App => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoPhone.Context }, - AppDataBaseType.User => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoUser.Context }, - AppDataBaseType.Ext => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoExt.Context }, - AppDataBaseType.Game => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoGame.Context }, - _ => throw new Exception("未找到对应的数据连接") - }; - } + var (cacheList, dbType) = NamespaceMapping[namespaceKey]; + object cacheLock = GetOrAddCacheLock(typeLock, cacheList); + CacheBaseConfig cacheBaseConfig = cloudGamingBase.ToCacheBaseConfig(dbType); - /// - /// 日志方法 - /// - private static void Log(string message) + return new DataBaseEntityCache(cacheBaseConfig, cacheLock, expWhere: expWhere, cacheTime: cacheTime); + } + + /// + /// 获取或添加缓存锁对象 + /// + private static object GetOrAddCacheLock(Type typeLock, ConcurrentDictionary cacheList) + { + return cacheList.GetOrAdd(typeLock, _ => { - // 替换为其他日志记录系统,例如 NLog, Serilog 等 - Console.WriteLine(message); - } + Log($"没有找到锁对象 ==> {typeLock.Name}"); + return new object(); + }); + } + + /// + /// 根据数据库类型生成缓存配置 + /// + public static CacheBaseConfig ToCacheBaseConfig(this CloudGamingBase cloudGamingBase, AppDataBaseType appDataBaseType) + { + return appDataBaseType switch + { + AppDataBaseType.App => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoPhone.Context }, + AppDataBaseType.User => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoUser.Context }, + AppDataBaseType.Ext => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoExt.Context }, + AppDataBaseType.Game => new CacheBaseConfig { AppConfig = cloudGamingBase.AppConfig, Mapper = cloudGamingBase.Mapper, DbContext = cloudGamingBase.Dao.DaoGame.Context }, + _ => throw new Exception("未找到对应的数据连接") + }; + } + + /// + /// 日志方法 + /// + private static void Log(string message) + { + // 替换为其他日志记录系统,例如 NLog, Serilog 等 + //Console.WriteLine(message); } } diff --git a/src/CloudGaming/Code/CloudGaming.Code/Cache/Special/ImageEntityCache.cs b/src/CloudGaming/Code/CloudGaming.Code/Cache/Special/ImageEntityCache.cs index 3e69260..e6dc019 100644 --- a/src/CloudGaming/Code/CloudGaming.Code/Cache/Special/ImageEntityCache.cs +++ b/src/CloudGaming/Code/CloudGaming.Code/Cache/Special/ImageEntityCache.cs @@ -1,213 +1,210 @@ -using AutoMapper; - -using CloudGaming.Code.AppExtend; using CloudGaming.Code.DataAccess; -using CloudGaming.DtoModel.Game; using HuanMeng.DotNetCore.CacheHelper; using HuanMeng.DotNetCore.CacheHelper.Contract; -using HuanMeng.DotNetCore.Redis; - -using Newtonsoft.Json; using StackExchange.Redis; -using System; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static System.Net.Mime.MediaTypeNames; +namespace CloudGaming.Code.Cache.Special; -namespace CloudGaming.Code.Cache.Special +/// +/// 图片缓存表 +/// +/// +/// +/// +/// +/// +public class ImageEntityCache(DAO dao, AppConfig appConfig, IDatabase database, AppRequestConfig appRequestConfig) : ICacheClearData, ICacheReloadData { /// - /// 图片缓存表 + /// 是否加载过图片 /// - /// - /// - /// - /// - /// - public class ImageEntityCache(DAO dao, AppConfig appConfig, IDatabase database, AppRequestConfig appRequestConfig) : ICacheClearData, ICacheReloadData + public static bool IsLoadImage { get; set; } = false; + /// + /// 本地内存图片缓存key + /// + private string key = $"{appConfig.Identifier}:App:Image"; + /// + /// redis图片缓存key + /// + private string redisKey = $"App:Image"; + /// + /// 当前使用的图片key + /// + ConcurrentDictionary>? ImageData { get; set; } + + /// + /// + /// + /// + /// + public ConcurrentDictionary this[string language] { - public static bool IsLoadImage { get; set; } = false; - private string key = $"{appConfig.Identifier}:App:Image"; - private string redisKey = $"App:Image"; - ConcurrentDictionary>? ImageData { get; set; } - - /// - /// - /// - /// - /// - public ConcurrentDictionary this[string language] + get { - get + if (string.IsNullOrEmpty(language)) + { + language = appRequestConfig.Language; + } + if (ImageData != null && ImageData.TryGetValue(language, out var images)) { - if (string.IsNullOrEmpty(language)) - { - language = appRequestConfig.Language; - } - if (ImageData != null && ImageData.TryGetValue(language, out var images)) - { - return images; - } - ImageData = MemoryCacheHelper.GetCache>>(key); - if (ImageData == null) - { - ImageData = new ConcurrentDictionary>(); - - } - if (!ImageData.TryGetValue(language, out images)) - { - ImageData.TryAdd(language, images = new ConcurrentDictionary()); - MemoryCacheHelper.SetCache(key, ImageData, 60 * 60 * 24); - } - return images; - - - } - } - - /// - /// - /// - /// - /// - /// - public string this[string language, int imageId] - { - get + ImageData = MemoryCacheHelper.GetCache>>(key); + if (ImageData == null) { - if (imageId == 0) - { - return ""; - } - var _imageData = this[language]; - if (_imageData == null) - { - _imageData = new ConcurrentDictionary(); - } - if (!_imageData.TryGetValue(imageId, out var imageUrl)) - { - var imageValue = database.StringGet($"{redisKey}:{language}:{imageId}"); - if (imageValue.IsNullOrEmpty) - { - imageValue = database.StringGet($"{redisKey}:default:{imageId}"); - } - if (!IsLoadImage && imageValue.IsNullOrEmpty) - { - if (!database.KeyExists(redisKey)) - { - ImageData = LoadImage(dao, appConfig); - MemoryCacheHelper.SetCache(key, ImageData, 60 * 60 * 24); - IsLoadImage = true; - _imageData = ImageData[language]; - if (!_imageData.TryGetValue(imageId, out imageUrl)) - { - imageUrl = ""; - } - return imageUrl; - } - } - imageUrl = imageValue; - _imageData.TryAdd(imageId, imageUrl); - //if (!_imageData.TryGetValue(imageId, out imageUrl)) - //{ - // if (string.IsNullOrEmpty(imageUrl)) - // { - // imageUrl = ""; - // } + ImageData = new ConcurrentDictionary>(); - //}//imageValue.ToString(); - - //MemoryCacheHelper.SetCache(key, ImageData, 60 * 60 * 24); - - } - return imageUrl; } - } - - public ConcurrentDictionary> LoadImage(DAO dao, AppConfig appConfig) - { - // 初始化_data字典 - ConcurrentDictionary> _data = new ConcurrentDictionary>(); - - // 获取图像列表 - var imageList = dao.DaoExt.Context.T_App_Image - .Where(it => !string.IsNullOrEmpty(it.Url)) - .AsNoTracking() - .Select(it => new { it.ImageId, it.Language, it.Url }) - .ToList(); - - // 设置默认语言 - if (string.IsNullOrEmpty(appConfig.DefaultLanguage)) + if (!ImageData.TryGetValue(language, out images)) { - appConfig.DefaultLanguage = "zh"; + ImageData.TryAdd(language, images = new ConcurrentDictionary()); + //内存中缓存 + MemoryCacheHelper.SetCache(key, ImageData, 60 * 60 * 24); } - - // 创建默认语言的图像字典 - var defaultImage = imageList - .Where(it => it.Language == appConfig.DefaultLanguage) - .GroupBy(it => it.ImageId) - .ToDictionary(group => group.Key, group => appConfig.AliyunConfig.ImagePrefix + group.Last().Url); - - // 遍历图像列表,填充_data字典 - foreach (var item in imageList) - { - // 尝试获取语言字典,如果不存在则创建 - if (!_data.TryGetValue(item.Language, out var languageImage)) - { - languageImage = new ConcurrentDictionary(); - _data[item.Language] = languageImage; - } - - // 设置图像URL - languageImage[item.ImageId] = appConfig.AliyunConfig.ImagePrefix + item.Url; - - // 如果默认图像字典中没有此ImageId,加入默认图像字典 - if (!defaultImage.ContainsKey(item.ImageId)) - { - defaultImage[item.ImageId] = appConfig.AliyunConfig.ImagePrefix + item.Url; - } - } - - // 更新默认语言的图像字典 - _data["default"] = new ConcurrentDictionary(defaultImage); - foreach (var item in _data.Keys) - { - foreach (var image in _data[item]) - { - string _redisKey = $"{redisKey}:{item}:{image.Key}"; - database.StringSet(_redisKey, image.Value, TimeSpan.FromDays(1)); - } - } - database.StringSet(redisKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromDays(1)); - return _data; - } - - public string this[int imageId] - { - get - { - var imageUrl = this[appRequestConfig.Language, imageId]; - return imageUrl; - } - } - - public bool ClearData() - { - throw new NotImplementedException(); - } - - public void ReloadData() - { - throw new NotImplementedException(); + return images; } } + + /// + /// + /// + /// + /// + /// + public string this[string language, int imageId] + { + get + { + if (imageId == 0) + { + return ""; + } + var _imageData = this[language]; + if (_imageData == null) + { + _imageData = new ConcurrentDictionary(); + } + if (!_imageData.TryGetValue(imageId, out var imageUrl)) + { + var imageValue = database.StringGet($"{redisKey}:{language}:{imageId}"); + if (imageValue.IsNullOrEmpty) + { + imageValue = database.StringGet($"{redisKey}:default:{imageId}"); + } + if (!IsLoadImage && imageValue.IsNullOrEmpty) + { + if (!database.KeyExists(redisKey)) + { + ImageData = LoadImage(dao, appConfig); + MemoryCacheHelper.SetCache(key, ImageData, 60 * 60 * 24); + IsLoadImage = true; + _imageData = ImageData[language]; + if (!_imageData.TryGetValue(imageId, out imageUrl)) + { + imageUrl = ""; + } + return imageUrl; + } + } + imageUrl = imageValue; + _imageData.TryAdd(imageId, imageUrl); + } + return imageUrl; + } + } + + public ConcurrentDictionary> LoadImage(DAO dao, AppConfig appConfig) + { + // 初始化_data字典 + ConcurrentDictionary> _data = new ConcurrentDictionary>(); + + // 获取图像列表 + var imageList = dao.DaoExt.Context.T_App_Image + .Where(it => !string.IsNullOrEmpty(it.Url)) + .AsNoTracking() + .Select(it => new { it.ImageId, it.Language, it.Url }) + .ToList(); + + // 设置默认语言 + if (string.IsNullOrEmpty(appConfig.DefaultLanguage)) + { + appConfig.DefaultLanguage = "zh"; + } + + // 创建默认语言的图像字典 + var defaultImage = imageList + .Where(it => it.Language == appConfig.DefaultLanguage) + .GroupBy(it => it.ImageId) + .ToDictionary(group => group.Key, group => appConfig.AliyunConfig.ImagePrefix + group.Last().Url); + + // 遍历图像列表,填充_data字典 + foreach (var item in imageList) + { + // 尝试获取语言字典,如果不存在则创建 + if (!_data.TryGetValue(item.Language, out var languageImage)) + { + languageImage = new ConcurrentDictionary(); + _data[item.Language] = languageImage; + } + + // 设置图像URL + languageImage[item.ImageId] = appConfig.AliyunConfig.ImagePrefix + item.Url; + + // 如果默认图像字典中没有此ImageId,加入默认图像字典 + if (!defaultImage.ContainsKey(item.ImageId)) + { + defaultImage[item.ImageId] = appConfig.AliyunConfig.ImagePrefix + item.Url; + } + } + + // 更新默认语言的图像字典 + _data["default"] = new ConcurrentDictionary(defaultImage); + foreach (var item in _data.Keys) + { + foreach (var image in _data[item]) + { + string _redisKey = $"{redisKey}:{item}:{image.Key}"; + database.StringSet(_redisKey, image.Value, TimeSpan.FromDays(1)); + } + } + database.StringSet(redisKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromDays(1)); + return _data; + } + + /// + /// 读取图片 + /// + /// + /// + public string this[int imageId] + { + get + { + var imageUrl = this[appRequestConfig.Language, imageId]; + return imageUrl; + } + } + + /// + /// 清除缓存 + /// + /// + /// + public bool ClearData() + { + throw new NotImplementedException(); + } + + /// + /// 重新加载缓存 + /// + /// + public void ReloadData() + { + throw new NotImplementedException(); + } }