private void RefreshCache(long SectorNumber) { if (SectorCached == SectorNumber) { return; } if (SectorCached != -1 && CommitCacheRequired) { CommitCacheRequired = false; StoreGGSector(SectorCached, Cache); } var data = LoadGGSector(SectorNumber); try { unsafe { fixed(byte *ptrBuffer = &data[0]) { Cache = (GGSector)Marshal.PtrToStructure(new IntPtr(ptrBuffer), typeof(GGSector)); } } SectorCached = SectorNumber; } catch { SectorCached = -1; } }
private void StoreGGSector(long sectorNumber, GGSector sector) { var fileName = GetFileName(); using (var sw = File.OpenWrite(fileName)) { var data = new byte[512]; var fileSector = sectorNumber; // % SectorsPerFile; sw.Seek(fileSector * 512 + 512, SeekOrigin.Begin); unsafe { fixed(byte *ptrBuffer = &data[0]) { Marshal.StructureToPtr(sector, new IntPtr(ptrBuffer), true); } } sw.Write(data, 0, data.GetLength(0)); sw.Close(); Repository.TransactionLog.SectorTouch(fileName, (int)fileSector); } }