public unsafe void Set(string url, long etag, BlittableJsonReaderObject result) { var mem = _unmanagedBuffersPool.Allocate(result.Size); result.CopyTo((byte *)mem.Address); if (Interlocked.Add(ref _totalSize, result.Size) > _maxSize) { if (_cleanupTask == null) { var cleanup = new Task(FreeSpace); if (Interlocked.CompareExchange(ref _cleanupTask, cleanup, null) == null) { cleanup.ContinueWith(_ => Interlocked.Exchange(ref _cleanupTask, null)); cleanup.Start(); } } } var httpCacheItem = new HttpCacheItem { Usages = 1, Etag = etag, Ptr = (byte *)mem.Address, Size = result.Size, Allocation = mem, LastServerUpdate = SystemTime.UtcNow, Cache = this, }; _items.AddOrUpdate(url, httpCacheItem, (s, oldItem) => { oldItem.Release(); return(httpCacheItem); }); }
public unsafe void Set(string url, string changeVector, BlittableJsonReaderObject result) { var mem = _unmanagedBuffersPool.Allocate(result.Size); result.CopyTo(mem.Address); if (Interlocked.Add(ref _totalSize, result.Size) > _maxSize && _isFreeSpaceRunning.Raise()) { Task.Run(() => FreeSpace()); } var httpCacheItem = new HttpCacheItem { Usages = 1, ChangeVector = changeVector, Ptr = mem.Address, Size = result.Size, Allocation = mem, LastServerUpdate = SystemTime.UtcNow, Cache = this, Generation = Generation }; _items.AddOrUpdate(url, httpCacheItem, (s, oldItem) => { oldItem.Release(); return(httpCacheItem); }); }
private static string GetEncodedFilename(IntPtr processHandle, ref MEMORY_BASIC_INFORMATION memoryBasicInformation) { var memData = BuffersPool.Allocate(2048); var pFilename = memData.Address; try { int stringLength; stringLength = GetMappedFileName(processHandle, memoryBasicInformation.BaseAddress.ToPointer(), pFilename, 2048); if (stringLength == 0) { return(null); } var foundRelevantFilename = false; foreach (var item in RelevantFilesPostFixes) { fixed(byte *pItem = item) { if (stringLength < item.Length || Memory.Compare(pItem, pFilename + stringLength - item.Length, item.Length) != 0) { continue; } foundRelevantFilename = true; break; } } if (foundRelevantFilename == false) { return(null); } return(Encodings.Utf8.GetString(pFilename, stringLength)); } finally { BuffersPool.Return(memData); } }
public unsafe void Set(string url, string changeVector, BlittableJsonReaderObject result) { #if DEBUG result.BlittableValidation(); #endif var mem = _unmanagedBuffersPool.Allocate(result.Size); result.CopyTo(mem.Address); if (Interlocked.Add(ref _totalSize, result.Size) > _maxSize) { if (_isFreeSpaceRunning == false) { Task.Run(FreeSpace); } } var httpCacheItem = new HttpCacheItem { ChangeVector = changeVector, Ptr = mem.Address, Size = result.Size, Allocation = mem, Cache = this, Generation = Generation }; HttpCacheItem old = null; _items.AddOrUpdate(url, httpCacheItem, (s, oldItem) => { old = oldItem; return(httpCacheItem); }); //We need to check if the cache is been disposed after the item was added otherwise we will run into another race condition //where it started been disposed right after we checked it and before we managed to insert the new cache item. if (_disposing) { //We might have double release here but we have a protection for that. httpCacheItem.ReleaseRef(); } old?.ReleaseRef(); }