初始化
This commit is contained in:
105
Common/Cache/Cache.cs
Normal file
105
Common/Cache/Cache.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Common/Cache/MyValueSerializer.cs
Normal file
32
Common/Cache/MyValueSerializer.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FASTER.core;
|
||||
using System.Xml.Linq;
|
||||
using CommonEntity.CacheEntity;
|
||||
using MemoryPack;
|
||||
|
||||
namespace Common.Cache
|
||||
{
|
||||
|
||||
public class MyValueSerializer<T> : BinaryObjectSerializer<T>
|
||||
{
|
||||
public override void Serialize(ref T value)
|
||||
{
|
||||
//writer.Write(IPAddress.HostToNetworkOrder(Q.Length)); // 4字节长度头
|
||||
var Q = MemoryPackSerializer.Serialize(value);
|
||||
writer.Write(Q.Length); // 4字节长度头
|
||||
writer.Write(Q);
|
||||
}
|
||||
|
||||
public override void Deserialize(out T value)
|
||||
{
|
||||
int length = reader.ReadInt32(); // 读长度头
|
||||
byte[] buffer = reader.ReadBytes(length); // 读数据体
|
||||
value = MemoryPackSerializer.Deserialize<T>(buffer); // 反序列化
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user