Ejemplo n.º 1
0
        public Editor(UnsafeEntry entry, DiskLruCache parent)
        {
            this.parent = parent;

            Entry   = entry;
            written = entry.UnsafeReadable ? null : new bool[parent.ValueCount];
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens the cache in {@code directory}, creating a cache if none exists there.
        /// </summary>
        /// <param name="directory">a writable directory</param>
        /// <param name="appVersion">???</param>
        /// <param name="valueCount">the number of values per cache entry. Must be positive.</param>
        /// <param name="maxSize">the maximum number of bytes this cache should use to store</param>
        /// <param name="compactThreshold">Since how many operations cache will Collect</param>
        /// <returns></returns>
        public static DiskLruCache Open(DirectoryInfo directory, int appVersion, int valueCount, long maxSize, int compactThreshold = 2000)
        {
            if (maxSize <= 0)
            {
                throw new ArgumentException("maxSize <= 0");
            }

            if (valueCount <= 0)
            {
                throw new ArgumentException("valueCount <= 0");
            }

            var backupFile = new FileInfo(Path.Combine(directory.FullName, JournalFileBackup));

            // If a bkp file exists, use it instead.
            if (backupFile.Exists)
            {
                var journalFile = new FileInfo(Path.Combine(directory.FullName, JournalFile));

                // If journal file also exists just delete backup file.
                if (journalFile.Exists)
                {
                    backupFile.Delete();
                }
                else
                {
                    RenameTo(backupFile, journalFile, false);
                }
            }

            var cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, compactThreshold);

            if (cache.journalFile.Exists)
            {
                try {
                    cache.ReadJournal();
                    cache.ProcessJournal();

                    return(cache);
                } catch (IOException) {
                    // Journal is corrupt
                    cache.Delete();
                }
            }

            // Create a new empty cache.
            System.IO.Directory.CreateDirectory(directory.FullName);
            cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, compactThreshold);
            cache.RebuildJournal();

            return(cache);
        }
Ejemplo n.º 3
0
        public Snapshot(string key, long sequenceNumber, Stream[] readStreams, long[] lengths, DiskLruCache parent)
        {
            this.key            = key;
            this.sequenceNumber = sequenceNumber;
            this.readStreams    = readStreams;
            this.lengths        = lengths;

            parentRef = new WeakReference <DiskLruCache>(parent);
        }