提问人:ShishankJain 提问时间:11/3/2023 最后编辑:ShishankJain 更新时间:11/3/2023 访问量:61
如何在 C# 中使用 memcached 来存储和检索高达 1gb 的数据?
How can I use memcached in C# to store and retrieve data upto 1gb?
问:
我正在保存一个数据表,然后尝试从 memcache 获取数据。 我尝试使用命令增加max_bytes
memcached.exe -d start -m 1024
我正在使用以下代码保存数据:
public void SaveDataTableInMemoryCache(string key, DataTable dataTable)
{
var config = new MemcachedClientConfiguration();
config.AddServer("localhost", 11211);
using (var memcachedClient = new MemcachedClient(config))
{
byte[] searchSavedData = memcachedClient.Get<byte[]>(key);
if (searchSavedData != null)
{
memcachedClient.Remove(key);
}
// Set an expiration time of 24 hours (1 day)
TimeSpan expirationTime = TimeSpan.FromHours(24);
// Serialize the DataTable into a byte array before storing it in Memcached
byte[] serializedData = SerializeDataTable(dataTable);
// Store the serialized DataTable in Memcached with a 24-hour expiration time
memcachedClient.Store(StoreMode.Set, key, serializedData, expirationTime);
}
}
private byte[] SerializeDataTable(DataTable dataTable)
{
DataSet dataSet = new DataSet();
dataSet.Tables.Add(dataTable);
using (var ms = new System.IO.MemoryStream())
{
dataSet.WriteXml(ms, XmlWriteMode.WriteSchema); // Include schema
return ms.ToArray();
}
}
public DataTable RetrieveDataTableFromCache(string key)
{
var config = new MemcachedClientConfiguration();
config.AddServer("localhost", 11211);
using (var memcachedClient = new MemcachedClient(config))
{
// Retrieve the serialized DataTable from Memcached
byte[] serializedData = memcachedClient.Get<byte[]>(key);
if (serializedData != null)
{
// Deserialize the byte array back into a DataTable
return DeserializeDataTable(serializedData);
}
} // DataTable not found in cache
return null;
}
并尝试从 memcache 获取数据。它适用于少于 200 行和 15 列的数据表。
我怎样才能把这个带到 1gb 的数据表?
答: 暂无答案
评论
-m <num> --- Use <num> MB memory max to use for object storage
-m 512