C# 在Azure缓存中存储大于8MB的对象的技术

C# 在Azure缓存中存储大于8MB的对象的技术,c#,performance,azure,azure-storage-blobs,azure-caching,C#,Performance,Azure,Azure Storage Blobs,Azure Caching,对于如何在Azure缓存中存储大于8MB的对象,有人有什么建议吗。在我的例子中,我使用byte[]在blob中存储文件。但是,如果我能以某种方式将字节[]分割成更小的块,并将其存储为部分文件,然后在从缓存中检索文件后执行合并 伪柯德: 储存 bs <- split file into byte[] that are smaller than 8MB s <- string[bs.Length] foreach bs with index i s[i] <- name o

对于如何在Azure缓存中存储大于8MB的对象,有人有什么建议吗。在我的例子中,我使用byte[]在blob中存储文件。但是,如果我能以某种方式将字节[]分割成更小的块,并将其存储为部分文件,然后在从缓存中检索文件后执行合并

伪柯德:

储存

bs <- split file into byte[] that are smaller than 8MB
s <- string[bs.Length]
foreach bs with index i
    s[i] <- name of bs[i]
    Add bs[i] to Azure cache using s[i] as key

Add s to cache

bs经过几个小时的工作,我发现可以将较大的文件拆分为较小的文件,并将其存储到Azure缓存中。我想和你分享代码

用于拆分和连接字节[]的类

    public class CacheHelper
    {
        private const int kMaxFileSize = 8000000;
        private readonly int fFileSize;
        private readonly string fFileName;
        public CacheHelper(int sizeOfFile, string nameOfFile)
        {
            fFileSize = sizeOfFile;
            fFileName = nameOfFile;
        }

        public CachingObjectHolder Split(byte[] file)
        {
            var remainingSize = file.Length;
            var partialList = new List<byte[]>();
            var partial = new byte[file.Length > kMaxFileSize ? kMaxFileSize : file.Length];
            for (int i = 0; i < file.Length; i++)
            {
                if (i % kMaxFileSize == 0 && i > 0)
                {
                    partialList.Add(partial);
                    partial = new byte[remainingSize > kMaxFileSize ? kMaxFileSize : remainingSize];
                }

                partial[i % kMaxFileSize] = file[i];
                remainingSize--;
            }

            partialList.Add(partial);

            return new CachingObjectHolder(fFileName, partialList);
        }

        public static byte[] Join(CachingObjectHolder cachingObjectHolder)
        {
            var totalByteSize = cachingObjectHolder.Partials.Sum(x => x.Length);
            var output = new byte[totalByteSize];
            var globalCounter = 0;
            for (int i = 0; i < cachingObjectHolder.Partials.Count; i++)
            {
                for (int j = 0; j < cachingObjectHolder.Partials[i].Length; j++)
                {
                    output[globalCounter] = cachingObjectHolder.Partials[i][j];
                    globalCounter++;
                }
            }

            return output;
        }

        public static byte[] CreateFile(int size)
        {
            var tempFile = Path.GetTempFileName();
            using (var stream = new FileStream(tempFile, FileMode.OpenOrCreate))
            {
                using (var memStream = new MemoryStream())
                {
                    stream.SetLength(size);
                    stream.CopyTo(memStream);
                    return memStream.ToArray();
                }
            }
        }
    }
公共类CacheHelper
{
私有const int kMaxFileSize=8000000;
私有只读int fFileSize;
私有只读字符串fFileName;
公共CacheHelper(int-sizeOfFile,string-nameOfFile)
{
fFileSize=大小办公室;
fFileName=文件名;
}
公共CachingObjectHolder拆分(字节[]文件)
{
var remainingSize=file.Length;
var partialList=新列表();
var partial=新字节[file.Length>kMaxFileSize?kMaxFileSize:file.Length];
for(int i=0;i0)
{
partialList.Add(部分);
partial=新字节[remainingSize>kMaxFileSize?kMaxFileSize:remainingSize];
}
部分[i%kMaxFileSize]=文件[i];
剩余尺寸--;
}
partialList.Add(部分);
返回新的CachingObjectHolder(fFileName,partialList);
}
公共静态字节[]联接(CachingObjectHolder CachingObjectHolder)
{
var totalByteSize=cachingObjectHolder.Partials.Sum(x=>x.Length);
var输出=新字节[totalByteSize];
var globalCounter=0;
for(int i=0;i
以下是与Azure缓存通信的代码

    public class Cache
    {
        private const string kFileListName = "FileList";

        public static DataCacheFactory DataCacheFactory
        {
            get
            {
                return new DataCacheFactory();
            }
        }

        private static DataCache fDataCache;
        public static DataCache DataCache
        {
            get
            {
                if(fDataCache == null)
                {
                    fDataCache = DataCacheFactory.GetDefaultCache();
                }

                return fDataCache;
            }
        }

        public static byte[] Get(string name)
        {
            var dic = GetFileList();
            if (dic == null)
            {
                return (byte[])DataCache.Get(name);
            }
            if (dic.ContainsKey(name))
            {
                var list = dic[name];
                var input = new List<byte[]>();
                var cache = DataCache;
                list = list.OrderBy(x => x.Item2).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    input.Add(cache.Get(list[i].Item1) as byte[]);
                }

                var holder = new CachingObjectHolder(name, input);
                return CacheHelper.Join(holder);
            }
            else
            {
                return (byte[])DataCache.Get(name);
            }
        }

        public static void Put(string name, byte[] file)
        {
            if (file.Length > CacheHelper.kMaxFileSize)
            {
                var helper = new CacheHelper(file.Length, name);
                var output = helper.Split(file);
                var dic = GetFileList();
                if (dic == null)
                {
                    dic = new Dictionary<string, List<Tuple<string, int>>>();
                }

                var partials = new List<Tuple<string, int>>();
                for (int i = 0; i < output.CachingObjects.Count; i++)
                {
                    DataCache.Add(output.CachingObjects[i].Name, output.Partials[output.CachingObjects[i].Index]);
                    partials.Add(new Tuple<string, int>(output.CachingObjects[i].Name, 
                                               output.CachingObjects[i].Index));   
                }

                dic.Add(name, partials.OrderBy(x => x.Item2).ToList());
                PutFileList(dic);
            }
            else
            {
                DataCache.Add(name, file);
            }
        }

        public static void Remove(string name)
        {
            var dic = GetFileList();
            if (dic == null)
            {
                DataCache.Remove(name);
                return;
            }

            if (dic.ContainsKey(name))
            {
                var list = dic[name];
                for (int i = 0; i < list.Count; i++)
                {
                    DataCache.Remove(list[i].Item1);
                }

                dic.Remove(name);
                PutFileList(dic);
            }
            else
            {
                DataCache.Remove(name);
            }
        }

        private static void PutFileList(Dictionary<string, List<Tuple<string, int>>> input)
        {
            DataCache.Put(kFileListName, input);
        }

        private static Dictionary<string, List<Tuple<string, int>>> GetFileList()
        {
            return DataCache.Get(kFileListName) as Dictionary<string, List<Tuple<string, int>>>;
        }
    }
公共类缓存
{
private const string kFileListName=“FileList”;
公共静态DataCacheFactory DataCacheFactory
{
收到
{
返回新的DataCacheFactory();
}
}
私有静态数据缓存fDataCache;
公共静态数据缓存数据缓存
{
收到
{
如果(fDataCache==null)
{
fDataCache=DataCacheFactory.GetDefaultCache();
}
返回fDataCache;
}
}
公共静态字节[]获取(字符串名称)
{
var dic=GetFileList();
如果(dic==null)
{
返回(字节[])DataCache.Get(名称);
}
if(dic.ContainsKey(名称))
{
var list=dic[名称];
var输入=新列表();
var cache=DataCache;
list=list.OrderBy(x=>x.Item2.ToList();
for(int i=0;iCacheHelper.kMaxFileSize)
{
var helper=新的CacheHelper(file.Length,name);
var输出=helper.Split(文件);
var dic=GetFileList();
如果(dic==null)
{
dic=新字典();
}
var partials=新列表();
for(int i=0;ix.Item2.ToList());
文件列表(dic);
}
其他的
{
添加(名称、文件);
}
}
公共静态无效删除(字符串名称)
{
var dic=GetFileList();
如果(dic==null)
{
DataCache.Remove(名称);
回来
}
if(dic.ContainsKey(名称))
{
var list=dic[名称];
for(int i=0;i    public class Cache
    {
        private const string kFileListName = "FileList";

        public static DataCacheFactory DataCacheFactory
        {
            get
            {
                return new DataCacheFactory();
            }
        }

        private static DataCache fDataCache;
        public static DataCache DataCache
        {
            get
            {
                if(fDataCache == null)
                {
                    fDataCache = DataCacheFactory.GetDefaultCache();
                }

                return fDataCache;
            }
        }

        public static byte[] Get(string name)
        {
            var dic = GetFileList();
            if (dic == null)
            {
                return (byte[])DataCache.Get(name);
            }
            if (dic.ContainsKey(name))
            {
                var list = dic[name];
                var input = new List<byte[]>();
                var cache = DataCache;
                list = list.OrderBy(x => x.Item2).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    input.Add(cache.Get(list[i].Item1) as byte[]);
                }

                var holder = new CachingObjectHolder(name, input);
                return CacheHelper.Join(holder);
            }
            else
            {
                return (byte[])DataCache.Get(name);
            }
        }

        public static void Put(string name, byte[] file)
        {
            if (file.Length > CacheHelper.kMaxFileSize)
            {
                var helper = new CacheHelper(file.Length, name);
                var output = helper.Split(file);
                var dic = GetFileList();
                if (dic == null)
                {
                    dic = new Dictionary<string, List<Tuple<string, int>>>();
                }

                var partials = new List<Tuple<string, int>>();
                for (int i = 0; i < output.CachingObjects.Count; i++)
                {
                    DataCache.Add(output.CachingObjects[i].Name, output.Partials[output.CachingObjects[i].Index]);
                    partials.Add(new Tuple<string, int>(output.CachingObjects[i].Name, 
                                               output.CachingObjects[i].Index));   
                }

                dic.Add(name, partials.OrderBy(x => x.Item2).ToList());
                PutFileList(dic);
            }
            else
            {
                DataCache.Add(name, file);
            }
        }

        public static void Remove(string name)
        {
            var dic = GetFileList();
            if (dic == null)
            {
                DataCache.Remove(name);
                return;
            }

            if (dic.ContainsKey(name))
            {
                var list = dic[name];
                for (int i = 0; i < list.Count; i++)
                {
                    DataCache.Remove(list[i].Item1);
                }

                dic.Remove(name);
                PutFileList(dic);
            }
            else
            {
                DataCache.Remove(name);
            }
        }

        private static void PutFileList(Dictionary<string, List<Tuple<string, int>>> input)
        {
            DataCache.Put(kFileListName, input);
        }

        private static Dictionary<string, List<Tuple<string, int>>> GetFileList()
        {
            return DataCache.Get(kFileListName) as Dictionary<string, List<Tuple<string, int>>>;
        }
    }
    public class CachingObjectHolder
    {
        public readonly List<byte[]> Partials;
        public readonly List<CachingObject> CachingObjects;
        public readonly string CacheName;

        public CachingObjectHolder(string name, List<byte[]> partialList)
        {
            Partials = partialList;
            CacheName = name;
            CachingObjects = new List<CachingObject>();
            CreateCachingObjects();
        }

        private void CreateCachingObjects()
        {
            for (int i = 0; i < Partials.Count; i++)
            {
                CachingObjects.Add(new CachingObject(string.Format("{0}_{1}", CacheName, i), i));
            }
        }
    }

    public class CachingObject
    {
        public int Index { get; set; }
        public string Name { get; set; }

        public CachingObject(string name, int index)
        {
            Index = index;
            Name = name;
        }
    }