示例#1
0
        public static byte[] ReadAllBytes(string filename, FileInfoCache fileInfoCache)
        {
            var cacheName = CacheName.Normalize(filename);

            using (CreateLock(cacheName))
            {
                DateTime time = fileInfoCache != null?fileInfoCache.GetFileInfo(cacheName).LastWriteTimeUtc : new FileInfo(cacheName).LastWriteTimeUtc;

                lock (_cache)
                {
                    if (!_cache.TryGetValue(cacheName, out var bytes))
                    {
                        var b = File.ReadAllBytes(cacheName);
                        _cache.Add(cacheName, new CacheBlob <byte[]>(time, b));

                        return(b);
                    }
                    else if (bytes.CacheTime != time)
                    {
                        bytes.CacheTime = time;
                        return(bytes.Value = File.ReadAllBytes(cacheName));
                    }
                    else
                    {
                        return(bytes.Value);
                    }
                }
            }
        }
示例#2
0
 private static CrossProcessLock CreateLock(string filename) =>
 new CrossProcessLock($"FileCache_{CacheName.Sanitize(filename)}");