/// <summary> /// Remove a cached image from the cache /// </summary> /// <param name="result">The item to remove</param> public void RemoveCachedResult(CachedSearchResults result) { lock (threadLock) { CachedResults.Remove(result); } }
private async Task <IImmutableList <DescribeGroupsResponse.Group> > UpdateGroupMetadataFromServerAsync(IEnumerable <string> groupIds, bool ignoreCache, CancellationToken cancellationToken) { return(await _groupSemaphore.LockAsync( async() => { var cachedResults = new CachedResults <DescribeGroupsResponse.Group>(misses: groupIds); if (!ignoreCache) { cachedResults = CachedResults <DescribeGroupsResponse.Group> .ProduceResults(cachedResults.Misses, groupId => TryGetCachedGroup(groupId, Configuration.CacheExpiration)); if (cachedResults.Misses.Count == 0) { return cachedResults.Hits; } } Log.Info(() => LogEvent.Create($"Router refreshing metadata for groups {string.Join(",", cachedResults.Misses)}")); var request = new DescribeGroupsRequest(cachedResults.Misses); var response = await this.SendToAnyAsync(request, cancellationToken).ConfigureAwait(false); UpdateGroupCache(response); // since the above may take some time to complete, it's necessary to hold on to the groups we found before // just in case they expired between when we searched for them and now. var result = cachedResults.Hits.AddNotNullRange(response?.groups); return result; }, cancellationToken).ConfigureAwait(false)); }
/// <summary> /// Returns a single colum from a table. /// </summary> private CachedResults <T> GetFromSingleColumn <T>(string table, string column) { CachedResults <T> results; using (var conn = new SQLiteConnection(connString)) { conn.Open(); using (var trans = conn.BeginTransaction()) { var sql = $"SELECT COUNT(*) FROM {table}"; var cmd = new SQLiteCommand(sql, conn); var count = (long)cmd.ExecuteScalar(); results = new CachedResults <T>(count); if (count == 0) { return(results); } sql = $"SELECT {column} FROM {table}"; cmd = new SQLiteCommand(sql, conn); var reader = cmd.ExecuteReader(); for (var i = 0; reader.Read(); i++) { results.Results[i] = (T)reader[column]; } trans.Commit(); } } return(results); }
/// <summary> /// If <see cref="UseCache"/> is set to true, clears the cache of this MathConverter object; If <see cref="UseCache"/> is false, this method does nothing. /// </summary> public void ClearCache() { if (UseCache) { CachedResults.Clear(); } }
/// <inheritdoc /> public async Task <IImmutableList <MetadataResponse.Topic> > GetTopicMetadataAsync(IEnumerable <string> topicNames, CancellationToken cancellationToken) { var cachedResults = CachedResults <MetadataResponse.Topic> .ProduceResults(topicNames, topicName => TryGetCachedTopic(topicName)); return(cachedResults.Misses.Count == 0 ? cachedResults.Hits : cachedResults.Hits.AddRange(await UpdateTopicMetadataFromServerAsync(cachedResults.Misses, false, cancellationToken).ConfigureAwait(false))); }
/// <inheritdoc /> public async Task <IImmutableList <DescribeGroupsResponse.Group> > GetGroupMetadataAsync(IEnumerable <string> groupIds, CancellationToken cancellationToken) { var cachedResults = CachedResults <DescribeGroupsResponse.Group> .ProduceResults(groupIds, groupId => TryGetCachedGroup(groupId)); return(cachedResults.Misses.Count == 0 ? cachedResults.Hits : cachedResults.Hits.AddRange(await UpdateGroupMetadataFromServerAsync(cachedResults.Misses, false, cancellationToken).ConfigureAwait(false))); }
/// <summary> /// Determines if a cache is valid, and either refreshes the cache or returns the cached results. /// </summary> private T[] GetSingleColumnCachedResults <T>(CachedResults <T> cached, string table, string column) { if (cached == null || !cached.Fresh) { cached = GetFromSingleColumn <T>(table, column); } return(cached.Results); }
/// <inheritdoc /> public IImmutableList <DescribeGroupsResponse.Group> GetGroupMetadata(IEnumerable <string> groupIds) { var cachedResults = CachedResults <DescribeGroupsResponse.Group> .ProduceResults(groupIds, groupId => TryGetCachedGroup(groupId)); if (cachedResults.Misses.Count > 0) { throw new RoutingException($"No metadata defined for groups: {string.Join(",", cachedResults.Misses)}"); } return(ImmutableList <DescribeGroupsResponse.Group> .Empty.AddRange(cachedResults.Hits)); }
/// <inheritdoc /> public IImmutableList <MetadataResponse.Topic> GetTopicMetadata(IEnumerable <string> topicNames) { var cachedResults = CachedResults <MetadataResponse.Topic> .ProduceResults(topicNames, topicName => TryGetCachedTopic(topicName)); if (cachedResults.Misses.Count > 0) { throw new RoutingException($"No metadata defined for topics: {string.Join(",", cachedResults.Misses)}"); } return(ImmutableList <MetadataResponse.Topic> .Empty.AddRange(cachedResults.Hits)); }
public static void SetCachedSolveResults(string key, string jsonResults, GrasshopperDefinition definition) { if (string.IsNullOrWhiteSpace(key)) { return; } var cache = new CachedResults { Definition = definition, WatchedFileRuntimeSerialNumber = GrasshopperDefinition.WatchedFileRuntimeSerialNumber, Json = jsonResults }; System.Runtime.Caching.MemoryCache.Default.Add(key, cache, CachePolicy); }
/// <summary> /// Adds a single value to a table with a single column. /// </summary> private void AddToSingleColumn <T>(string table, string column, T value, CachedResults <T> cache) { using (var conn = new SQLiteConnection(connString)) { conn.Open(); using (var trans = conn.BeginTransaction()) { var sql = $"INSERT INTO {table} ({column}) VALUES(@value)"; var cmd = new SQLiteCommand(sql, conn); cmd.Parameters.Add(new SQLiteParameter("value", value)); cmd.ExecuteNonQuery(); trans.Commit(); } } if (cache != null) { cache.Fresh = false; } Logger.LogLine($"Added {column} {value} to {table}"); }
protected IEnumerator <T> RunQuery() { if (CachedResults != null && ShouldReuseResultset) { return(CachedResults.GetEnumerator()); } if (QueryText == null) { var sb = new StringBuilder(); CreateSelectQuery(sb); QueryText = sb.ToString(); } IRdfConnection <T> conn = QueryFactory.CreateConnection(this); IRdfCommand <T> cmd = conn.CreateCommand(); cmd.ElideDuplicates = Expressions.ContainsKey("Distinct") || Expressions.ContainsKey("Reduced"); cmd.CommandText = QueryText; cmd.InstanceName = GetInstanceName(); return(cmd.ExecuteQuery()); }
private async Task <IImmutableList <MetadataResponse.Topic> > UpdateTopicMetadataFromServerAsync(IEnumerable <string> topicNames, bool ignoreCache, CancellationToken cancellationToken) { return(await _topicSemaphore.LockAsync( async() => { var cachedResults = new CachedResults <MetadataResponse.Topic>(misses: topicNames); if (!ignoreCache) { cachedResults = CachedResults <MetadataResponse.Topic> .ProduceResults(cachedResults.Misses, topicName => TryGetCachedTopic(topicName, Configuration.CacheExpiration)); if (cachedResults.Misses.Count == 0) { return cachedResults.Hits; } } MetadataRequest request; MetadataResponse response; if (ignoreCache && topicNames == null) { Log.Info(() => LogEvent.Create("Router refreshing metadata for all topics")); request = new MetadataRequest(); response = await this.GetMetadataAsync(request, cancellationToken).ConfigureAwait(false); } else { Log.Info(() => LogEvent.Create($"Router refreshing metadata for topics {string.Join(",", cachedResults.Misses)}")); request = new MetadataRequest(cachedResults.Misses); response = await this.GetMetadataAsync(request, cancellationToken).ConfigureAwait(false); } if (response != null) { await UpdateConnectionCacheAsync(response.brokers, cancellationToken); } UpdateTopicCache(response); // since the above may take some time to complete, it's necessary to hold on to the topics we found before // just in case they expired between when we searched for them and now. var result = cachedResults.Hits.AddNotNullRange(response?.topic_metadata); return result; }, cancellationToken).ConfigureAwait(false)); }
public bool Check(AbstractConnection connection, ILogger logger) { if (CachedResults.ContainsKey(connection.Name)) { return(CachedResults[connection.Name]); } if (!new FileInfo(connection.Server).Exists) { logger.Warn("{0} not found.", connection.Server); var type = System.Type.GetType("System.Data.SqlServerCe.SqlCeEngine, System.Data.SqlServerCe", false, true); dynamic engine = System.Activator.CreateInstance(type, connection.GetConnectionString()); engine.CreateDatabase(); logger.Warn("Created {0} database file.", connection.Server); } ; return(CheckConnection(connection)); }
/// <summary> /// Adds a wallpaper to the cache /// </summary> /// <param name="wallpaper">Wallpaper to add</param> /// <param name="savedpath">The full path to the image file</param> public void AddCachedResults(Wallpaper wallpaper, string savedpath) { if (wallpaper == null || !File.Exists(savedpath)) { return; } if (CacheSize() > Settings.Instance.MaxCacheSize) { return; } lock (threadLock) { CachedSearchResults item; for (int i = 0; i < CachedResults.Count; i++) { item = CachedResults[i]; //If the wallpaper is already in the cache just refresh its age. //That way duplicates are avoided and if (item.Wallpaper.id == wallpaper.id) { item.ResultDate = DateTime.Now; return; } } CachedSearchResults newitem = new CachedSearchResults(); newitem.Path = savedpath; newitem.ResultDate = DateTime.Now; newitem.Wallpaper = wallpaper; CachedResults.Add(newitem); } }
/// <summary> /// Delete all images and the metadata (cache) for all images over the specified age /// </summary> /// <param name="age">The max age to keep. (Ie any items older than this will be deleted)</param> public void PurgeCachedResults(TimeSpan age) { lock (threadLock) { for (int i = 0; i < CachedResults.Count * 0.3; i++) { TimeSpan itemAge = DateTime.Now - CachedResults[i].ResultDate; if (itemAge > age) { try { File.Delete(CachedResults[i].Path); } catch (Exception e) { Console.WriteLine("Settings.PurgeCachedResults: " + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace); } CachedResults.RemoveAt(i); i--; } } } }
/// <summary> /// Removes a single value from a table with a single column. /// </summary> private void RemoveFromSingleColumn <T>(string table, string column, T value, CachedResults <T> cache) { using (var conn = new SQLiteConnection(connString)) { conn.Open(); using (var trans = conn.BeginTransaction()) { var sql = $"DELETE FROM {table} WHERE {column} = @value"; var cmd = new SQLiteCommand(sql, conn); cmd.Parameters.Add(new SQLiteParameter("value", value)); cmd.ExecuteNonQuery(); trans.Commit(); } } if (cache != null) { cache.Fresh = false; } Logger.LogLine($"Removed {column} {value} from {table}"); }