public AssetBase Get(string id, out bool found) { m_assetRequests[id].Amt++; m_Requests++; AssetBase asset = null; found = false; bool forceMemCache = m_assetRequests[id].Amt > _forceMemoryCacheAmount; if ((m_MemoryCacheEnabled || forceMemCache) && m_MemoryCache.TryGetValue(id, out asset)) { found = true; m_MemoryHits++; } else { string filename = GetFileName(id); if (File.Exists(filename)) { try { asset = ExtractAsset(id, asset, filename, forceMemCache); found = true; if (asset == null) { m_assetRequests[id].Amt = _forceMemoryCacheAmount; } } catch (Exception e) { LogException(e); // If there was a problem deserializing the asset, the asset may // either be corrupted OR was serialized under an old format // {different version of AssetBase} -- we should attempt to // delete it and re-cache File.Delete(filename); } } #if WAIT_ON_INPROGRESS_REQUESTS // Check if we're already downloading this asset. If so, try to wait for it to // download. if (m_WaitOnInprogressTimeout > 0) { m_RequestsForInprogress++; ManualResetEvent waitEvent; if (m_CurrentlyWriting.TryGetValue(filename, out waitEvent)) { waitEvent.WaitOne(m_WaitOnInprogressTimeout); return(Get(id)); } } #else // Track how often we have the problem that an asset is requested while // it is still being downloaded by a previous request. if (m_CurrentlyWriting.Contains(filename)) { m_RequestsForInprogress++; } #endif } if (((m_logLevel >= 1)) && (m_HitRateDisplay != 0) && (m_Requests % m_HitRateDisplay == 0)) { m_HitRateFile = (double)m_DiskHits / m_Requests * 100.0; MainConsole.Instance.InfoFormat("[FLOTSAM ASSET CACHE]: Cache Get :: {0} :: {1}", id, asset == null ? "Miss" : "Hit"); MainConsole.Instance.InfoFormat("[FLOTSAM ASSET CACHE]: File Hit Rate {0}% for {1} requests", m_HitRateFile.ToString("0.00"), m_Requests); if (m_MemoryCacheEnabled) { m_HitRateMemory = (double)m_MemoryHits / m_Requests * 100.0; MainConsole.Instance.InfoFormat("[FLOTSAM ASSET CACHE]: Memory Hit Rate {0}% for {1} requests", m_HitRateMemory.ToString("0.00"), m_Requests); } MainConsole.Instance.InfoFormat( "[FLOTSAM ASSET CACHE]: {0} unnessesary requests due to requests for assets that are currently downloading.", m_RequestsForInprogress); } if (_assetMonitor != null) { _assetMonitor.AddAsset(asset); } return(asset); }
public AssetBase Get(string id) { m_Requests++; AssetBase asset = null; if (m_MemoryCacheEnabled && m_MemoryCache.TryGetValue(id, out asset)) { m_MemoryHits++; } else { string filename = GetFileName(id); lock (m_fileCacheLock) { if (File.Exists(filename)) { try { string file = File.ReadAllText(filename); asset = new AssetBase(); asset.Unpack(OpenMetaverse.StructuredData.OSDParser.DeserializeJson(file)); UpdateMemoryCache(id, asset); m_DiskHits++; } catch (SerializationException e) { LogException(e); // If there was a problem deserializing the asset, the asset may // either be corrupted OR was serialized under an old format // {different version of AssetBase} -- we should attempt to // delete it and re-cache File.Delete(filename); } catch (Exception e) { LogException(e); } } } #if WAIT_ON_INPROGRESS_REQUESTS // Check if we're already downloading this asset. If so, try to wait for it to // download. if (m_WaitOnInprogressTimeout > 0) { m_RequestsForInprogress++; ManualResetEvent waitEvent; if (m_CurrentlyWriting.TryGetValue(filename, out waitEvent)) { waitEvent.WaitOne(m_WaitOnInprogressTimeout); return(Get(id)); } } #else // Track how often we have the problem that an asset is requested while // it is still being downloaded by a previous request. if (m_CurrentlyWriting.Contains(filename)) { m_RequestsForInprogress++; } #endif } if (((m_logLevel >= 1)) && (m_HitRateDisplay != 0) && (m_Requests % m_HitRateDisplay == 0)) { m_HitRateFile = (double)m_DiskHits / m_Requests * 100.0; MainConsole.Instance.InfoFormat("[FLOTSAM ASSET CACHE]: Cache Get :: {0} :: {1}", id, asset == null ? "Miss" : "Hit"); MainConsole.Instance.InfoFormat("[FLOTSAM ASSET CACHE]: File Hit Rate {0}% for {1} requests", m_HitRateFile.ToString("0.00"), m_Requests); if (m_MemoryCacheEnabled) { m_HitRateMemory = (double)m_MemoryHits / m_Requests * 100.0; MainConsole.Instance.InfoFormat("[FLOTSAM ASSET CACHE]: Memory Hit Rate {0}% for {1} requests", m_HitRateMemory.ToString("0.00"), m_Requests); } MainConsole.Instance.InfoFormat( "[FLOTSAM ASSET CACHE]: {0} unnessesary requests due to requests for assets that are currently downloading.", m_RequestsForInprogress); } IAssetMonitor monitor = (IAssetMonitor) m_simulationBase.ApplicationRegistry.RequestModuleInterface <IMonitorModule>().GetMonitor("", MonitorModuleHelper . AssetMonitor); if (monitor != null) { monitor.AddAsset(asset); } return(asset); }