Files
2025-12-11 14:04:39 +08:00

106 lines
4.0 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommonEntity.CacheEntity;
using FASTER.core;
namespace Common.Cache
{
public class Cache
{
private static System.Timers.Timer _checkpointTimer;
public static FasterKVSettings<string, MyData_Cache> settings { get; set; }
public static FasterKV<string, MyData_Cache> store { get; set; }
public static ConcurrentBag<ClientSession<string, MyData_Cache, MyData_Cache, MyData_Cache, Empty, IFunctions<string, MyData_Cache, MyData_Cache, MyData_Cache, Empty>>> _sessions = new();
static Cache()
{
settings = new FasterKVSettings<string, MyData_Cache>()
{
LogDevice = Devices.CreateLogDevice("c:/youtemp/log"), // 元数据日志
ObjectLogDevice = Devices.CreateLogDevice("c:/youtemp/obj"), // 对象数据日志
TryRecoverLatest = true,
ValueSerializer = () => new MyValueSerializer<MyData_Cache>()
};
store = new FasterKV<string, MyData_Cache>(settings);
var session1 = store.NewSession(new SimpleFunctions<string, MyData_Cache>());
var session2 = store.NewSession(new SimpleFunctions<string, MyData_Cache>());
var session3 = store.NewSession(new SimpleFunctions<string, MyData_Cache>());
var session4 = store.NewSession(new SimpleFunctions<string, MyData_Cache>());
var session5 = store.NewSession(new SimpleFunctions<string, MyData_Cache>());
_sessions.Add(session1);
_sessions.Add(session2);
_sessions.Add(session3);
_sessions.Add(session4);
_sessions.Add(session5);
_checkpointTimer = new System.Timers.Timer();
_checkpointTimer.Interval = 60000; // 1分钟做一次检查点
_checkpointTimer.Elapsed += _checkpointTimer_Elapsed;
_checkpointTimer.Start();
}
private async static void _checkpointTimer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
try
{
if (_sessions != null)
{
await store.TakeFullCheckpointAsync(CheckpointType.FoldOver);
}
}
catch (Exception)
{
}
}
public static async Task SetCache(string Key, MyData_Cache ValueData)
{
bool isexits = _sessions.TryTake(result: out var TakeSession);
if (isexits)
{
TakeSession.Upsert(ref Key, ref ValueData);
_sessions.Add(TakeSession);
await TakeSession.CompletePendingAsync(true);
}
else
{
var NewSession = store.NewSession(new SimpleFunctions<string, MyData_Cache>());
var status = NewSession.Upsert(ref Key, ref ValueData);
_sessions.Add(NewSession);
}
//await store.TakeFullCheckpointAsync(CheckpointType.FoldOver);
}
public static async Task<MyData_Cache> GetCache(string Key)
{
MyData_Cache output = null;
bool isexits = _sessions.TryTake(result: out var TakeSession);
if (isexits)
{
_sessions.Add(TakeSession);
TakeSession.Refresh();
var status = await TakeSession.ReadAsync(ref Key, ref output);
MyData_Cache output1 = status.Output;
output = output1;
}
else
{
var NewSession = store.NewSession(new SimpleFunctions<string, MyData_Cache>());
_sessions.Add(NewSession);
var status = await NewSession.ReadAsync(ref Key, ref output);
MyData_Cache output1 = status.Output;
output = output1;
}
return output;
}
}
}