public override void Invoke(AMFContext context) { MessageOutput messageOutput = context.MessageOutput; if ((FluorineConfiguration.Instance.CacheMap != null) && (FluorineConfiguration.Instance.CacheMap.Count > 0)) { for (int i = 0; i < context.AMFMessage.BodyCount; i++) { AMFBody bodyAt = context.AMFMessage.GetBodyAt(i); if ((messageOutput.GetResponse(bodyAt) == null) && !bodyAt.IsEmptyTarget) { string target = bodyAt.Target; IList parameterList = bodyAt.GetParameterList(); string cacheKey = CacheMap.GenerateCacheKey(target, parameterList); if (FluorineConfiguration.Instance.CacheMap.ContainsValue(cacheKey)) { object content = FluorineConfiguration.Instance.CacheMap.Get(cacheKey); if ((log != null) && log.get_IsDebugEnabled()) { log.Debug(__Res.GetString("Cache_HitKey", new object[] { bodyAt.Target, cacheKey })); } CachedBody body = new CachedBody(bodyAt, content); messageOutput.AddBody(body); } } } } }
public bool FileExists(string filename) { // get our lock lock ( locker ) { // first make sure it's in our cahce. If not, // we want to consider it not existing. It was orphaned. // (Maybe we crashed before we could save?) if (CacheMap.ContainsKey(filename)) { // validate it does exist on disk if (File.Exists(CachePath + "/" + filename)) { // and return return(true); } else { // otherwise, it didn't exist on disk, but // was somehow in our cache. Get rid of it. CacheMap.Remove(filename); } } return(false); } }
public DropboxCachedFile(DokanFileInfo dokanInfo, Logger log = null) { var appContext = dokanInfo.TryGetApplicationContext(); _dokanInfo = dokanInfo; _client = appContext.DropNetClient; _metaData = appContext.MetaData; if (log != null) { _log = log; } else { _log = LogManager.CreateNullLogger(); LogManager.DisableLogging(); } _file = appContext.FileNameComponents; var path = string.Format(@"temp\{0}", _dokanInfo.DokanContext); Directory.CreateDirectory(Path.GetDirectoryName(path)); _innerStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 512, FileOptions.DeleteOnClose | FileOptions.RandomAccess); _innerStream.SetLength(_metaData.Bytes); _log.Trace("File length is {0}", _metaData.Bytes); _cacheMap = new CacheMap(); }
public void CacheMap_IsCached() { var cm = new CacheMap(); cm.SetCached(0, 10); Assert.IsTrue(cm.IsCached(0, 10)); Assert.IsTrue(cm.IsCached(2, 5)); }
protected override bool Remove(TKey key) { if (CacheMap.TryGetValue(key, out var node)) { _distinctDictionary.Remove(node.Value.Value); } return(base.Remove(key)); }
public bool SaveFile(object buffer, string filename, TimeSpan?expirationTime = null) { // sync point so we don't read multiple times at once. // Note: If we ever need to support multiple threads reading from the cache at once, we'll need // a table to hash multiple locks per filename. This serializes all file loads across all threads. lock ( locker ) { bool result = false; try { // attempt to write the file out to disk using (FileStream writer = new FileStream(CachePath + "/" + filename, FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter( ); StreamWriter mapStream = new StreamWriter(writer); formatter.Serialize(mapStream.BaseStream, buffer); mapStream.Dispose( ); // store in our cachemap the filename and when we wrote it. // Don't ever add it twice. If it exists, remove it if (CacheMap.Contains(filename)) { CacheMap.Remove(filename); Rock.Mobile.Util.Debug.WriteLine(string.Format("{0} is already cached. Updating time to {1}", filename, DateTime.Now)); } else { Rock.Mobile.Util.Debug.WriteLine(string.Format("Adding {0} to cache. Time {1}", filename, DateTime.Now)); } if (expirationTime.HasValue == false) { expirationTime = CacheFileDefaultExpiration; } // and now it'll be added a second time. CacheMap.Add(filename, DateTime.Now + expirationTime.Value); // if an exception occurs we won't set result to true result = true; writer.Close( ); writer.Dispose( ); } } catch (Exception e) { Rock.Mobile.Util.Debug.WriteLine(e.Message); } // return the result return(result); } }
public void RemoveFile(string filename) { // take our lock lock ( locker ) { // delete the entry File.Delete(CachePath + "/" + filename); CacheMap.Remove(filename); } }
public void CacheMap_GetOneMisingChunk() { var cm = new CacheMap(); cm.SetCached(0, 32); var missing = cm.GetMissingChunks(33, 10); Assert.AreEqual(1, missing.Count); Assert.AreEqual(new CachedFileChunk(33, 10), missing[0]); }
public void CacheMap_GetMisingChunks() { var cm = new CacheMap(); cm.SetCached(5, 4); // overlaps with the second one cm.SetCached(1, 9); // overlaps with previous one cm.SetCached(20, 10); // has a gap before cm.SetCached(15, 1); // is adjascent to the next one cm.SetCached(16, 1); // is adjascent to the next one var missing = cm.GetMissingChunks(0, 31); Assert.AreEqual(4, missing.Count); Assert.AreEqual(new CachedFileChunk(new Range <int>(0, 0)), missing[0]); Assert.AreEqual(new CachedFileChunk(new Range <int>(10, 14)), missing[1]); Assert.AreEqual(new CachedFileChunk(new Range <int>(17, 19)), missing[2]); Assert.AreEqual(new CachedFileChunk(new Range <int>(30, 30)), missing[3]); }
/// <summary> /// Build caches usign the specified configuration. /// The caches are returned in a <see cref="CacheMap"/> /// </summary> /// <param name="cacheConfiguration">A dictionary mapping <see cref="CacheType"/> values /// to an <see cref="ICacheOptions"/> object. This specifies the <see cref="ICacheBuilder"/> /// and size parameter to use when constructing a cache of the associated type. /// </param> /// <returns>a <see cref="CacheMap"/> containing the created caches</returns> private static CacheMap BuildCaches(Dictionary <CacheType, ICacheOptions> cacheConfiguration) { CacheMap caches = new CacheMap(); foreach (var configuration in cacheConfiguration) { if (configuration.Value.Builder != null) { switch (configuration.Key) { case CacheType.StringsCache: caches.StringCache = configuration.Value.Builder.Build <int, AsciiString>( configuration.Value.Size); break; case CacheType.NodesCache: caches.NodeCache = configuration.Value.Builder.Build <int, Entities.Node>( configuration.Value.Size); break; case CacheType.ValuesCache: caches.ValueCache = configuration.Value.Builder.Build <int, Value>( configuration.Value.Size); break; case CacheType.ProfilesCache: caches.ProfileCache = configuration.Value.Builder.Build <int, Entities.Profile>( configuration.Value.Size); break; case CacheType.SignaturesCache: caches.SignatureCache = configuration.Value.Builder.Build <int, Signature>( configuration.Value.Size); break; default: break; } } } return(caches); }
/// <summary> /// Scans the cache hashtable and removes any entries that are expired. Additionally, it deletes the file /// from the cache folder. /// CAUTION: If you pass true, all files will automatically be erased /// </summary> public void CleanUp(bool forceEraseAll = false) { lock ( locker ) { Rock.Mobile.Util.Debug.WriteLine("Running cleanup"); List <DictionaryEntry> expiredItems = new List <DictionaryEntry>( ); // scan our cache and remove anything older than the expiration time. foreach (DictionaryEntry entry in CacheMap) { DateTime entryValue = (DateTime)entry.Value; // if it's older than our expiration time, delete it TimeSpan deltaTime = (DateTime.Now - entryValue); if (DateTime.Now >= entryValue || forceEraseAll == true) { // delete the entry File.Delete(CachePath + "/" + entry.Key); expiredItems.Add(entry); Rock.Mobile.Util.Debug.WriteLine(string.Format("{0} expired. Age: {1} minutes past expiration.", (string)entry.Key, deltaTime.TotalMinutes)); } else { Rock.Mobile.Util.Debug.WriteLine(string.Format("{0} still fresh NOT REMOVING.", (string)entry.Key)); } } // remove all entries that have been expired foreach (DictionaryEntry entry in expiredItems) { CacheMap.Remove(entry.Key); } Rock.Mobile.Util.Debug.WriteLine("Cleanup complete"); } }
public void TestRemoveEldest() { int i; int sz = 10; CacheMap lhm = new CacheMap(); for (i = 0; i < sz; i++) { int ii = i; lhm.Put(ii, i * 2); } Collection <Object> s1 = lhm.Values(); Iterator <Object> it1 = s1.Iterator(); Assert.IsTrue(lhm.Size() == s1.Size(), "Returned set of incorrect Size 1"); for (i = 5; it1.HasNext; i++) { int jj = (int)it1.Next(); Assert.IsTrue(jj == i * 2, "Returned incorrect entry set 1"); } Assert.IsTrue(!it1.HasNext, "Entries left in map"); }
/// <summary> /// Load the necessary values from the data file in to the DataSet. /// Initially, this will only load data such as file headers /// and the smaller lists. /// </summary> /// <param name="dataSet">The dataset object to load in to</param> /// <param name="cacheConfiguration">the cache configuration to use when creating the caches</param> private static void LoadForStreaming(IndirectDataSet dataSet, Dictionary <CacheType, ICacheOptions> cacheConfiguration) { var reader = dataSet.Pool.GetReader(); try { CacheMap cacheMap = BuildCaches(cacheConfiguration); dataSet.CacheMap = cacheMap; reader.BaseStream.Position = 0; CommonFactory.LoadHeader(dataSet, reader); // ---- Create AsciiString list var stringLoader = EntityLoaderFactory.GetLoaderFor( new Header(reader), cacheMap.StringCache, dataSet, new StreamAsciiStringFactory()); dataSet.Strings = new StreamList <AsciiString, IStreamDataSet>(stringLoader); MemoryFixedList <Component, DataSet> components = null; switch (dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV31: components = new MemoryFixedList <Component, DataSet>( dataSet, reader, new ComponentFactoryV31()); break; case BinaryConstants.FormatVersions.PatternV32: components = new MemoryFixedList <Component, DataSet>( dataSet, reader, new ComponentFactoryV32()); break; } dataSet._components = components; var maps = new MemoryFixedList <Map, DataSet>( dataSet, reader, new MapFactory()); dataSet._maps = maps; var properties = new PropertiesList( dataSet, reader, new PropertyFactory()); dataSet._properties = properties; // ---- Create Value list var valueLoader = EntityLoaderFactory.GetLoaderFor(new Header(reader), cacheMap.ValueCache, dataSet, new ValueFactory <IStreamDataSet>()); dataSet._values = new StreamList <Value, IStreamDataSet>(valueLoader); // ---- Create Profile list var profileLoader = EntityLoaderFactory.GetLoaderFor(new Header(reader), cacheMap.ProfileCache, dataSet, new ProfileStreamFactory(dataSet.Pool)); dataSet.Profiles = new StreamList <Entities.Profile, IStreamDataSet>(profileLoader); switch (dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV31: // ---- Create Signature list for V31 var signatureLoaderV31 = EntityLoaderFactory.GetLoaderFor( new Header(reader), cacheMap.SignatureCache, dataSet, new SignatureFactoryV31 <IndirectDataSet>(dataSet)); dataSet._signatures = new StreamList <Signature, IndirectDataSet>( signatureLoaderV31); break; case BinaryConstants.FormatVersions.PatternV32: // ---- Create Signature list for V32 var signatureLoaderV32 = EntityLoaderFactory.GetLoaderFor( new Header(reader), cacheMap.SignatureCache, dataSet, new SignatureFactoryV32 <IndirectDataSet>(dataSet)); dataSet._signatures = new StreamList <Signature, IndirectDataSet>( signatureLoaderV32); dataSet._signatureNodeOffsets = new IntegerList(dataSet, reader); dataSet._nodeRankedSignatureIndexes = new IntegerList(dataSet, reader); break; } dataSet._rankedSignatureIndexes = new IntegerList(dataSet, reader); switch (dataSet.VersionEnum) { case BinaryConstants.FormatVersions.PatternV31: // ---- Create Nodes list for V31 var nodeLoaderV31 = EntityLoaderFactory.GetLoaderFor( new Header(reader), cacheMap.NodeCache, dataSet, new NodeStreamFactoryV31(dataSet.Pool)); dataSet.Nodes = new StreamList <Entities.Node, IStreamDataSet>( nodeLoaderV31); break; case BinaryConstants.FormatVersions.PatternV32: // ---- Create Nodes list for V31 var nodeLoaderV32 = EntityLoaderFactory.GetLoaderFor( new Header(reader), cacheMap.NodeCache, dataSet, new NodeStreamFactoryV32(dataSet.Pool)); dataSet.Nodes = new StreamList <Entities.Node, IStreamDataSet>( nodeLoaderV32); break; } var rootNodes = new MemoryFixedList <Entities.Node, DataSet>( dataSet, reader, new RootNodeFactory()); dataSet.RootNodes = rootNodes; var profileOffsets = new MemoryFixedList <ProfileOffset, DataSet>( dataSet, reader, new ProfileOffsetFactory()); dataSet._profileOffsets = profileOffsets; // Read into memory all the small lists which are frequently accessed. reader.BaseStream.Position = components.Header.StartPosition; components.Read(reader); reader.BaseStream.Position = maps.Header.StartPosition; maps.Read(reader); reader.BaseStream.Position = properties.Header.StartPosition; properties.Read(reader); reader.BaseStream.Position = rootNodes.Header.StartPosition; rootNodes.Read(reader); reader.BaseStream.Position = profileOffsets.Header.StartPosition; profileOffsets.Read(reader); } finally { dataSet.Pool.Release(reader); } }
public override object Invoke(IMessage message) { object obj2 = null; string str3; MessageException innerException; RemotingMessage message2 = message as RemotingMessage; string operation = message2.operation; string fullTypeName = base.DestinationSettings.Properties["source"] as string; if ((message2.source != null) && (message2.source != string.Empty)) { if (fullTypeName == "*") { fullTypeName = message2.source; } if (fullTypeName != message2.source) { str3 = __Res.GetString("Type_MismatchMissingSource", new object[] { message2.source, base.DestinationSettings.Properties["source"] as string }); throw new MessageException(str3, new TypeLoadException(str3)); } } if (fullTypeName == null) { throw new TypeInitializationException("null", null); } string source = fullTypeName + "." + operation; IList body = message2.body as IList; string cacheKey = CacheMap.GenerateCacheKey(source, body); if (FluorineConfiguration.Instance.CacheMap.ContainsValue(cacheKey)) { obj2 = FluorineConfiguration.Instance.CacheMap.Get(cacheKey); if (obj2 != null) { if ((log != null) && log.get_IsDebugEnabled()) { log.Debug(__Res.GetString("Cache_HitKey", new object[] { operation, cacheKey })); } return(obj2); } } FactoryInstance factoryInstance = base.Destination.GetFactoryInstance(); factoryInstance.Source = fullTypeName; object obj3 = factoryInstance.Lookup(); if (obj3 == null) { throw new MessageException(new TypeInitializationException(fullTypeName, null)); } Type type = obj3.GetType(); if (!TypeHelper.GetTypeIsAccessible(type)) { str3 = __Res.GetString("Type_InitError", new object[] { type.FullName }); throw new MessageException(str3, new TypeLoadException(str3)); } try { MethodInfo methodInfo = MethodHandler.GetMethod(type, operation, body); if (methodInfo == null) { throw new MessageException(new MissingMethodException(fullTypeName, operation)); } object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RoleAttribute), true); if ((customAttributes != null) && (customAttributes.Length == 1)) { RoleAttribute attribute = customAttributes[0] as RoleAttribute; string[] roles = attribute.Roles.Split(new char[] { ',' }); if (!base.Destination.Service.DoAuthorization(roles)) { throw new UnauthorizedAccessException(__Res.GetString("Security_AccessNotAllowed")); } } ParameterInfo[] parameters = methodInfo.GetParameters(); object[] array = new object[parameters.Length]; body.CopyTo(array, 0); TypeHelper.NarrowValues(array, parameters); obj2 = new InvocationHandler(methodInfo).Invoke(obj3, array); } catch (TargetInvocationException exception) { innerException = null; if (exception.InnerException is MessageException) { innerException = exception.InnerException as MessageException; } else { innerException = new MessageException(exception.InnerException); } throw innerException; } catch (Exception exception3) { innerException = new MessageException(exception3); throw innerException; } if ((FluorineConfiguration.Instance.CacheMap != null) && FluorineConfiguration.Instance.CacheMap.ContainsCacheDescriptor(source)) { CacheableObject obj4 = new CacheableObject(source, cacheKey, obj2); FluorineConfiguration.Instance.CacheMap.Add(obj4.Source, obj4.CacheKey, obj4); obj2 = obj4; } return(obj2); }