Exemplo n.º 1
0
 /// <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);
     }
 }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
 /// <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();
     }
 }
Exemplo n.º 5
0
        /// <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)));
        }
Exemplo n.º 6
0
        /// <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)));
        }
Exemplo n.º 7
0
 /// <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);
 }
Exemplo n.º 8
0
        /// <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));
        }
Exemplo n.º 9
0
        /// <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));
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        /// <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}");
        }
Exemplo n.º 12
0
        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());
        }
Exemplo n.º 13
0
        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));
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 15
0
        /// <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);
            }
        }
Exemplo n.º 16
0
        /// <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--;
                    }
                }
            }
        }
Exemplo n.º 17
0
        /// <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}");
        }