Files
Web_BLSKafka_Server_Prod/MyFaster/LengRe.cs
2025-11-21 08:48:01 +08:00

99 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FASTER.core;
namespace MyFaster
{
public class LengRe
{
public void AAA()
{
// 创建存储设备(混合内存+磁盘)
var logDevice = Devices.CreateLogDevice("colddata.log"); // 冷数据存储到磁盘
var logSettings = new LogSettings
{
LogDevice = logDevice,
MemorySizeBits = 20, // 1MB 内存(热数据区)
PageSizeBits = 16 // 64KB 磁盘页
};
// 创建 FASTER 实例
var store = new FasterKV<Key, Value>(
size: 1 << 16, // 哈希表大小
logSettings: logSettings
);
// 创建会话
var session = store.NewSession(new SimpleFunctions<Key, Value>());
// 写入数据(自动处理热/冷数据)
Key key = new Key { Id = 1 };
Value value = new Value { Data = "示例数据", LastAccessed = DateTime.UtcNow };
session.Upsert(ref key, ref value);
// 读取数据(热数据在内存,冷数据从磁盘加载)
Value output = default;
Status status = session.Read(ref key, ref output);
Console.WriteLine($"读取数据: {output.Data}");
Console.WriteLine($"最后访问: {output.LastAccessed}");
// 更新访问时间(保持数据热度)
output.LastAccessed = DateTime.UtcNow;
session.Upsert(ref key, ref output);
// 填充测试数据
for (int i = 0; i < 1000; i++)
{
Key k = new Key { Id = i };
Value v = new Value { Data = $"Item-{i}", LastAccessed = DateTime.UtcNow };
session.Upsert(ref k, ref v);
}
// 访问部分数据保持热度
for (int i = 0; i < 10; i++) // 频繁访问前10条
{
Key k = new Key { Id = i };
Value v = default;
session.Read(ref k, ref v);
v.LastAccessed = DateTime.UtcNow;
session.Upsert(ref k, ref v);
}
// 此时:
// - 前10条是热数据在内存
// - 其余990条是冷数据在磁盘
}
public void Read()
{
}
}
public struct Key : IEquatable<Key>
{
public int Id;
public bool Equals(Key other) => Id == other.Id;
}
public struct Value
{
public string Data;
public DateTime LastAccessed;
}
}