diff --git a/.vs/EntityFrameworkCore.Cacheable/v15/.suo b/.vs/EntityFrameworkCore.Cacheable/v15/.suo index a54c590..93be501 100644 Binary files a/.vs/EntityFrameworkCore.Cacheable/v15/.suo and b/.vs/EntityFrameworkCore.Cacheable/v15/.suo differ diff --git a/EntityFrameworkCore.Cacheable/CachedQueryProvider.cs b/EntityFrameworkCore.Cacheable/CachedQueryProvider.cs deleted file mode 100644 index 3d3457d..0000000 --- a/EntityFrameworkCore.Cacheable/CachedQueryProvider.cs +++ /dev/null @@ -1,153 +0,0 @@ -using Microsoft.Extensions.Caching.Memory; -using Microsoft.Extensions.Internal; -using System; -using System.Collections.Generic; -using System.Data.HashFunction; -using System.Data.HashFunction.xxHash; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Linq.Expressions; -using System.Runtime.Serialization.Formatters.Binary; -using System.Text; -using System.Threading.Tasks; - -namespace EntityFrameworkCore.Cacheable -{ - public class CachedQueryProvider : IQueryProvider - { - private readonly IQueryable _query; - private static IHashFunction __hashFunction; - private static readonly Object __syncLock = new Object(); - private TimeSpan _timeToLive; - private Boolean _isCacheHit; - private IHashValue _hash; - private static IMemoryCache __cache = new MemoryCache(new MemoryCacheOptions() - { - Clock = new SystemClock(), - //ExpirationScanFrequency = TimeSpan.FromSeconds(5) - }); - - static CachedQueryProvider() - { - __hashFunction = xxHashFactory.Instance.Create(new xxHashConfig - { - HashSizeInBits = 64 - }); - } - - /// - /// Defines methods to create and execute queries that are described by an System.Linq.IQueryable object. - /// - /// The input EF query. - /// If you think the computed hash of the query is not enough, set this value. - /// Stores the debug information of the caching process. - /// Gets an EF query and returns its hash to store in the cache. - /// The Cache Service Provider. - public CachedQueryProvider(IQueryable query, TimeSpan timeToLive) - { - _query = query; - _timeToLive = timeToLive; - } - - /// - /// Stores the debug information of the caching process. - /// - public Boolean IsCacheHit => _isCacheHit; - - public IHashValue Hash => _hash; - - public IQueryable CreateQuery(Expression expression) - { - return _query.Provider.CreateQuery(expression); - } - - public IQueryable CreateQuery(Expression expression) - { - return _query.Provider.CreateQuery(expression); - } - - public object Execute(Expression expression) - { - return Materialize(expression, () => _query.Provider.Execute(expression)); - } - - public TResult Execute(Expression expression) - { - return (TResult)Materialize(expression, () => _query.Provider.Execute(expression)); - } - - /// - /// Executes the query represented by a specified expression tree to cache its results. - /// - /// An expression tree that represents a LINQ query. - /// How to run the query. - /// The value that results from executing the specified query. - public object Materialize(Expression expression, Func materializer) - { - //var sql = _query.ToSql(); - //var sql = expression.ToString(); - //_hash = __hashFunction.ComputeHash(sql); - - //var bla = expressionComparer.GetHashCode(expression); - //Debug.WriteLine(bla); - - _hash = GetCacheKey(expression); - - //lock (__syncLock) - //{ - if (__cache.TryGetValue(_hash, out object cacheResult)) - { - _isCacheHit = true; - return cacheResult; - } - else - { - _isCacheHit = false; - var queryResult = materializer(); - - __cache.Set(_hash, queryResult, _timeToLive); - return queryResult; - } - //} - } - - /// - /// Gets a cache key for a query. - /// - public IHashValue GetCacheKey(Expression expression) - { - // locally evaluate as much of the query as possible - expression = Evaluator.PartialEval(expression, CanBeEvaluatedLocally); - - // support local collections - expression = LocalCollectionExpander.Rewrite(expression); - - // use the string representation of the expression for the cache key - string key = expression.ToString(); - - var hash = __hashFunction.ComputeHash(key); - - return hash; - } - - Func CanBeEvaluatedLocally - { - get - { - return expression => - { - // don't evaluate parameters - if (expression.NodeType == ExpressionType.Parameter) - return false; - - // can't evaluate queries - if (typeof(IQueryable).IsAssignableFrom(expression.Type)) - return false; - - return true; - }; - } - } - } -} diff --git a/EntityFrameworkCore.Cacheable/EntityFrameworkCore.Cacheable.csproj b/EntityFrameworkCore.Cacheable/EntityFrameworkCore.Cacheable.csproj index e215ad2..7fb7123 100644 --- a/EntityFrameworkCore.Cacheable/EntityFrameworkCore.Cacheable.csproj +++ b/EntityFrameworkCore.Cacheable/EntityFrameworkCore.Cacheable.csproj @@ -120,7 +120,6 @@ -