1111
This commit is contained in:
7
Service/Application.Domain/Cache/BaseCache.cs
Normal file
7
Service/Application.Domain/Cache/BaseCache.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public class BaseCache
|
||||
{
|
||||
public static string BaseCacheKey = "BaseGameCache:{0}";
|
||||
public static string BaseCacheKeys = "BaseGameCache:{0}:{1}";
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Application.Domain
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
4
Service/Application.Domain/GlobalUsings.cs
Normal file
4
Service/Application.Domain/GlobalUsings.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
global using Photon.Core;
|
||||
global using SqlSugar;
|
||||
global using Application.Domain.Entity;
|
||||
global using Photon.Core.Redis;
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public interface IAreaService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取所有区服
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<List<game_area>> GetAreaData();
|
||||
/// <summary>
|
||||
/// 获取区服信息
|
||||
/// </summary>
|
||||
/// <param name="areaId"></param>
|
||||
/// <returns></returns>
|
||||
Task<game_area> GetAreaInfo(int areaId);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public class AreaService : IAreaService, ITransient
|
||||
{
|
||||
private readonly ISqlSugarClient _dbClient;
|
||||
private readonly IRedisCache _redisClient;
|
||||
|
||||
public AreaService(ISqlSugarClient dbClient, IRedisCache redisClient)
|
||||
{
|
||||
_dbClient = dbClient;
|
||||
_redisClient = redisClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有区服
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<List<game_area>> GetAreaData()
|
||||
{
|
||||
string key = string.Format(BaseCache.BaseCacheKey, "AreaData");
|
||||
if (await _redisClient.ExistsAsync(key))
|
||||
{
|
||||
return await _redisClient.GetAsync<List<game_area>>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_area>();
|
||||
var data = await db.Queryable<game_area>().ToListAsync();
|
||||
await _redisClient.SetAsync(key, data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<game_area> GetAreaInfo(int areaId)
|
||||
{
|
||||
string key = string.Format(BaseCache.BaseCacheKeys, "AreaData", areaId);
|
||||
if (await _redisClient.ExistsAsync(key))
|
||||
{
|
||||
return await _redisClient.GetAsync<game_area>(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
var db = _dbClient.AsTenant().GetConnectionWithAttr<game_area>();
|
||||
var data = await db.Queryable<game_area>().Where(it => it.areaId == areaId).SingleAsync();
|
||||
await _redisClient.SetAsync(key, data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ClearAreaCache(int areaId = 0)
|
||||
{
|
||||
string key = string.Format(BaseCache.BaseCacheKey, "AreaData");
|
||||
await _redisClient.DelAsync(key);
|
||||
if (areaId != 0)
|
||||
{
|
||||
key = string.Format(BaseCache.BaseCacheKeys, "AreaData", areaId);
|
||||
await _redisClient.DelAsync(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user