/// <summary> /// Creates an instance of XMLCache for this type. /// </summary> /// <typeparam name="T">Class the XMLCache object is caching</typeparam> /// <param name="cacheTime">Time in minutes to keep items in cache</param> /// <returns></returns> public XMLCache <T> Create <T>(TimeSpan cacheTime) where T : class { dynamic ret; if (this.xmlCache.TryGetValue(typeof(T), out ret)) { return(ret); } var subDir = Path.Combine(this.rootDirectory.FullName, MD5Checksum.Generate(typeof(T).FullName).ToString()); ret = new XMLCache <T>( new DirectoryInfo(subDir), cacheTime); return((XMLCache <T>) this.xmlCache.GetOrAdd(typeof(T), ret)); //Add to internal list. }
private void WriteSlice(int layerNum, int sliceNum) { MemoryStream slice = this.WorkTaskInfo[layerNum].Slices[sliceNum]; MD5Checksum sliceHash = MD5Checksum.Generate(slice); var currentLayer = this.WorkTaskInfo[layerNum]; // Check if we can reuse other layers position. for (int i = 0; i < this.WorkTaskInfo.Length; i++) { if (i == layerNum) { // Skip if it's current layer. continue; } var otherLayer = this.WorkTaskInfo[i]; if (otherLayer.SlicePosition == -1) { // If this layer have no written slices yet, skip it continue; } if (otherLayer.SlicePosition <= currentLayer.SlicePosition) { // If layers last position is identical, or less, than my last: Skip. // The Anarchy Online planet map viewer does not handle 'backtracking' the stream position too well. continue; } if (otherLayer.SliceHash != sliceHash) { // Not identical, skip. continue; } // Found acceptable, pre-existing slice. // Use this and return. currentLayer.FilePos[sliceNum] = otherLayer.SlicePosition; return; } // We have to write new slice to binfile. int position = (int)this.BinFile.Position; currentLayer.FilePos[sliceNum] = position; currentLayer.SlicePosition = position; currentLayer.SliceHash = sliceHash; slice.WriteTo(this.BinFile); }
/// <summary> /// Generates a hash from the provided identifiers /// </summary> /// <param name="identifiers"></param> /// <returns></returns> public static MD5Checksum GetChecksum(object[] identifiers) { if (identifiers == null) { throw new ArgumentNullException("identifiers"); } if (identifiers.Length == 0) { throw new ArgumentException("You must provide at least one identifier for the cache entry.", "identifiers"); } var sb = new StringBuilder(); foreach (var id in identifiers) { sb.Append(id.ToString()); } return(MD5Checksum.Generate(sb.ToString())); }
/// <summary> /// Add data to the image archive /// </summary> /// <param name="Key"></param> /// <param name="Bytes"></param> public void Add(string Key, byte[] Bytes) { lock (this) { if (this.archiveIndexKey.ContainsKey(Key)) { throw new Exception("Key already exists: " + Key); } MD5Checksum md5 = MD5Checksum.Generate(Bytes); ImageArchiveEntry entry; if (this.archiveIndexMD5.ContainsKey(md5)) { ImageArchiveEntry oldiae = this.archiveIndexMD5[md5]; entry = new ImageArchiveEntry { Key = Key, BytePositionStart = oldiae.BytePositionStart, Size = oldiae.Size, MD5 = md5 }; } else { entry = new ImageArchiveEntry { Key = Key, BytePositionStart = this.archiveFile.Position, Size = Bytes.Length, MD5 = md5 }; //Add the entry to the MD5 index. this.archiveIndexMD5.Add(md5, entry); //Add to memorystream this.archiveFile.Write(Bytes, 0, Bytes.Length); } //Add to key index this.archiveIndexKey.Add(Key, entry); } }
public XMLCache(string path, int duration, int timeout) { this.CacheTarget = new FileCacheTarget(new DirectoryInfo(Path.Combine(path, MD5Checksum.Generate(typeof(T).FullName).ToString()))); this.DefaultDuration = new TimeSpan(0, duration, 0); }