예제 #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
        public ICacheInfo Deserialize(Stream s)
        {
            var reader = new BinaryReader(s);

            if (s.Length < sizeof(uint) + (sizeof(int) * 4) ||
                reader.ReadUInt32() != _header ||
                new Version(
                    reader.ReadInt32(),
                    reader.ReadInt32(),
                    reader.ReadInt32(),
                    reader.ReadInt32()) !=
                _dependencyVersion)
            {
                return(PrimitiveCacheInfo.Outdated);
            }

            var sources = new FileCacheSource[reader.ReadInt32()];

            for (var i = 0; i < sources.Length; i++)
            {
                var name = reader.ReadString();
                sources[i] = new FileCacheSource(
                    name,
                    _fileInfoCache.GetFileInfo(name),
                    new DateTime(reader.ReadInt64()));
            }

            return(new FileCacheInfo(sources));
        }