Hi Tom,
Yes, there's a work-around, but TBH, it's a sheer pain - it's an overhead for the developer, for the underlying hardware and for the end user in terms of poor user experience.
Anyway, the work-around is to slice/split your object to be cached into smaller pieces of size around 7 mb and then storing it in the cache. In turn, you will have to also maintain equal number of keys associated to each piece. While fetching the object
back, you will have to use the collection of keys to fetch associated pieces and then compile the whole info back into a single object. Because of explicit serialiation / deserialization and overall processing involved the entire process becomes quite slow
but at this moment this is the workaround I am aware of! At first place, I would suggest see if you don't have to do this, that is try to store lesser size objects in the cache if possible. Anyway, followed is some code for your help associated to what I just
explained - HTH
private static CacheEntry SplitDataTable(string DataTableName, DataTable table)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, table);
byte[] b = ms.ToArray();
Collection<byte[]> cacheKeys = new Collection<byte[]>();
Collection<String> cacheKeyNames = new Collection<string>();
int buffersize = 1024 * 1024 * 7;
int length = b.Count();
byte[] cacheEntry = null;
int count = 0;
int cursorLocation = 0;
ms.Position = cursorLocation;
while ((length - cursorLocation) > buffersize)
{
cacheEntry = new byte[buffersize];
ms.Read(cacheEntry, 0, buffersize);
cursorLocation = (int)ms.Position;
cacheKeyNames.Add(DataTableName + "_" + count);
cacheKeys.Add(cacheEntry);
count++;
}
cacheEntry = new byte[length - cursorLocation];
int readcount = ms.Read(cacheEntry, 0, (length - cursorLocation));
cacheKeys.Add(cacheEntry);
cacheKeyNames.Add(DataTableName + "_" + count);
CacheDetails cd = new CacheDetails { CacheKeyValues = cacheKeys };
CacheMetadata cm = new CacheMetadata { KeyCount = cacheKeys.Count, CacheKeyNames = cacheKeyNames };
return new CacheEntry { MetaData = cm, SubKeys = cd };
}
private static DataTable JoinAndDeserialize(Collection<byte[]> cacheKeys)
{
IFormatter formatter = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
int count = 0;
while (count < cacheKeys.Count)
{
ms.Write(cacheKeys[count], 0, cacheKeys[count].Count());
count++;
}
ms.Position = 0;
DataTable dt = (DataTable)formatter.Deserialize(ms);
return dt;
}
If this answers your question, please Mark it as Answer. If this post is helpful, please vote as helpful.