aspnet / caching Goto Github PK
View Code? Open in Web Editor NEW[Archived] Libraries for in-memory caching and distributed caching. Project moved to https://github.com/aspnet/Extensions
License: Apache License 2.0
[Archived] Libraries for in-memory caching and distributed caching. Project moved to https://github.com/aspnet/Extensions
License: Apache License 2.0
@lodejard brought up a good point of the need of having a Connect method on the interface. They seem to be implementation details for the particular distributed cache implementation (ex: RedisCache). SqlServerCache and LocalCache(distributed MemoryCache) do not require them.
cc @Tratcher @muratg @Eilon @davidfowl
And regarding related issue #131, RedisCache can implement an IDisposable interface and in the dispose method can close the connections. Currently RedisCache is registered as singleton and the DI would call the Dispose method on the singleton instance(I verified this is happening)
C:\>sqlservercache
System.InvalidOperationException: The current runtime target framework is not compatible with 'app'.
Current runtime target framework: 'DNX,Version=v4.5.1 (dnx451)'
Version: 1.0.0-rc1-16231
Type: Clr
Architecture: x64
OS Name: Windows
OS Version: 10.0
Runtime Id: win10-x64
Please make sure the runtime matches a framework specified in project.json
at Microsoft.Dnx.ApplicationHost.DefaultHost.GetEntryPoint(String applicationName)
at Microsoft.Dnx.ApplicationHost.Program.ExecuteMain(DefaultHost host, String applicationName, String[] args)
at Microsoft.Dnx.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Dnx.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
at Microsoft.Dnx.Host.Bootstrapper.RunAsync(List`1 args, IRuntimeEnvironment env, String appBase, FrameworkName targetFramework)
at Microsoft.Dnx.Host.RuntimeBootstrapper.ExecuteAsync(String[] args, BootstrapperContext bootstrapperContext)
at Microsoft.Dnx.Host.RuntimeBootstrapper.Execute(String[] args, BootstrapperContext bootstrapperContext)
I look into the test project, How can I get all the keys?
The memory cache uses time-stamps based on DateTime. Security uses DateTimeOffset (and rounds to one second). Which one should memory cache be using and why?
Hi, I was looking at the caching code and I found some points which can be enhanced to avoid creating unnecessary objects. I'm referring to the class MemoryCache, the private field _entries can be a Dictionary<string, CacheEntry>
instead of anIDictionary<string, CacheEntry>
(since it's private and it's always initialized to a Dictionary<string, CacheEntry>
) thus saving some unnecessary memory allocation when iterating over Keys and Values. Also the RemoveEntries
method could be changed to accept a List
instead of IEnumerable<CacheEntry>
(since it's a private method always called with an instance of List<CacheEntry>
) saving 1 object allocation when iterating over the list of entries. This change will also be consistent with the method ExpirePriorityBucket
which already accept generic lists parameters.
It's not a huge improvement, but it's a fairly simple one at least IMHO.
Max.
These two are super slow to run:
These take way, waaay too long.
CacheTagHelpers in Mvc rely on this API for users to be able to set custom triggers as part of their code. It would be nice to have the type name be less obtuse. Maybe CacheEntry.CurrentContext
?
cc @DamianEdwards
Hi team,
There's a TableName parameter to define the table used by SQLCache.
I searched online, it seems I have to call DNX commands somehow to create tables. and there's other document about how to make DNX ready. I think it is too complex and unnecessary.
I guess the script for table (maybe some stiredprocedures and triggers) must be very straightforward. could you provide the whole TSQL for creating the table and other database objects in a file on github? So developers can simply run it by themselves?
Cheers
Gary
There are possible race conditions in CacheEntry between AttachTriggers and DetatchTriggers. Similar for the callback APIs. These need to be hardened to prevent potential stress/race crashes.
Some cache entries may be derived from data in other entries and as such should be expired when the dependent entry is expired. Create a trigger that can monitor one or more keys.
For any store there should be a disconnect function taking full responsibility of
some think like this
foreach(string key in distributeCache.keys)
{
}
Newing up the type looks ugly
Are there any plans to implement the MVC 5 VaryByParam
property on ResponseCache
and CacheProfile
?
Either I am not understanding the purpose of MemoryCache or I am doing something wrong.
My goal is to use it to cache items in memory across multiple requests in my mv6 app.
I check to see if its null, if it is then I get my stuff from db and set it in the Index of home controller, then I try to access that from a ajax call but i always get null.. here is my code..
public IActionResult Index()
{
IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
if (cache.Get("searchList") == null)
{
var connectionString = "...";
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
var SQL = "SELECT * FROM SearchView";
var reader = SqlHelper.ExecuteReader(conn, CommandType.Text, SQL, null);
List<SearchResult> results = new List<SearchResult>();
if (reader.HasRows)
{
while (reader.Read())
{
results.Add(new SearchResult
{
Type = reader.GetInt32(0),
Description = reader.GetString(1)
});
}
}
var result = cache.Set("searchList", results, new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.NeverRemove));
}
}
return View();
}
then in my access method for ajax
public IActionResult GetSearchResultUsingCache(string query)
{
IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
var all = cache.Get("searchList");
}
the variable "all" is always null
The Redis provider is the only one that doesn't target net45
. That would be great if we could fix that ๐
https://github.com/aspnet/Caching/blob/dev/src/Microsoft.Framework.Caching.Redis/project.json#L15
Just noticed that the SqlServerCacheOptions doesn't implement the IOptions interface following the same pattern used in the MemoryCacheOptions and RedisCacheOptions classes.
Is this a bug, or intentionally left like this?
private const string ContextLinkDataName = "klr.host.EntryLinkHelpers.ContextLink";
Not sure if there are others.
Is it possible to create multiple instances of the RedisCache class where each instance connects to a different server/instance of Redis?
As far as I can see that the RedisCache class doesn't support named options, hence I presume that all instances have to use the same configuration.
Thanks
They exist for IMemoryCache but not IDistributedCache. Be sure to say when it's required to call Connect and/or ConnectAsync, since most of the samples never call this method.
In the GetOrAdd scenario the object creation factory is called and the resulting value is cached. However, the factory may determine that there is no data available to cache. Today even null returned values are cached, so the factory needs some other way to indicate not to cache the value.
Proposal: Support a conditional factory that returns true/false indicating if there is something to add to the cache. e.g. bool TryGetOrAdd(string key, out object obj, Func<ICacheAddContext, out object, bool>)
.
Workaround: The factory could throw an exception to prevent caching, but this is ugly and expensive:
try
{
var result = cache.GetOrAdd(key, context =>
{
...
throw new InvalidOperationException("No data available to cache");
});
}
catch (InvalidOperationException) {}
There doesn't seem to be a good reason for it to be constrained to string and object could have a number of advantages, e.g.:
cc @lodejard @davidfowl and @anpete
Hello,
In ASP.NET 4, we can always get Cache instance by using HttpContext.Cache.
What's the similar solution for me to get this kind of global cache access in all my Controllers and Middlewares in ASP.NET 5?
Thanks lot
Gary
Is there any default value or expected behavior on expiration times? I feel like I'm missing something critical, but isn't new DistributedCacheEntryOptions()
on a Set
basically a guaranteed fuckup with a distributed cache if the client process dies midway?
Hey guys,
I have been trying to figure out the hierarchy around the caching framework.
So far everything seems to be a huge mess:
I have been trying to wrap a local in-memory cache which I should be able to replace by changing the cache which I am using. However, I find it quite difficult to use LocalCache because it requires a binary serializer which is an unnecessary overhead and there isn't a build-in one in .NET Core anyway. I cannot wrap directly MemoryCache because the interface it implements basically means that I will be stuck using an in-memory cache and I will not be able to switch at a later point.
I am sorry if I sound too harsh but the current architecture is a mess. Please fix/rewrite this architecture before you release a final version and we get stuck with something which cannot be changed because it will require breaking changes.
Error
at Microsoft.Extensions.Caching.Memory.CacheExtensions.TryGetValue[TItem](IMemoryCache cache, Object key, TItem& value) at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query) at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Remotion.Linq.QueryableBase`1.GetEnumerator() at Microsoft.Data.Entity.Internal.InternalDbSet`1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at ITN.TS.Administrator.Controllers.ContentController.AddContent(Int32 id, Int32 parent, Boolean viewMode) in D:\Givi\ITN.TS.FW\src\ITN.TS.Web\Modules\ITN.TS.Administrator\Controllers\ContentController.cs:line 84.
Spoke to @davidfowl and @lodejard offline. Their consensus is that putting it in Caching.Abstractions is wrong layering. There doesn't seem to be an alternate place to move it to at this point.
Need to rename assemblies and namespaces from Microsoft.Framework.*
to Microsoft.Extensions.*
.
Once the change is committed, please post an issue in the https://github.com/aspnet/Announcements/issues/ repo that links back to this issue.
@muratg : This is a tracking item for the feature I am currently working on. BTW, I couldn't find the milestone for Beta6 in the drop down.
We'll lose the call stack:
/cc @kichalla
Why not exist extension method on ServiceCollection to register RedisCache?
Good morning,
I was trying to implement Sessions and Caching using Redis.
The framework is dnx451 and the versions of the packages are:
"Microsoft.Framework.Caching.Redis": "1.0.0-rc1-15658",
"Microsoft.AspNet.Session": "1.0.0-rc1-final"
RedisCache implements the interface Microsoft.Framework.Caching.Distributed.IDistributedCache but in order to use RedisCache for session, it needs to implement Microsoft.Extensions.Caching.Distributed.IDistributedCache which has a very similar contract, but not exactly the same.
In order to acomplish that, I have implemented my own class as follows:
public class OwnRedisCache : RedisCache, IDistributedCache
{
public OwnRedisCache(IOptions optionsAccessor) : base(optionsAccessor)
{
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
base.Set(key, value, new Microsoft.Framework.Caching.Distributed.DistributedCacheEntryOptions());
}
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options)
{
return base.SetAsync(key, value, new Microsoft.Framework.Caching.Distributed.DistributedCacheEntryOptions());
}
}
Is it possible or are you planning to modify the RedisCache class so it implements both interfaces?
Code to a full implementation can be found on https://github.com/joaquin-corchero/MVC6RedisSessionAndCache
Cheers
MemoryCache has two constructors, MemoryCache()
and MemoryCache(ISystemClock clock, bool listenForMemoryPressure)
. The second one is just for testing purposes, but it causes problems with DI.
The following doesn't work because DI tries to call the second constructor:
app.UseServices(collection =>
{
collection.AddSingleton<IMemoryCache, MemoryCache>();
});
You have to do it this way instead:
app.UseServices(collection =>
{
collection.AddInstance<IMemoryCache>(new MemoryCache());
});
This isn't a huge problem yet because it doesn't take any services, but it may become a problem when we add telemetry support.
Because it sounds better?
More consistent with other repos AddXyz
The Microsoft.Framework.Expiration.Interfaces
namespace has just one type: IExpirationTrigger
And what a weird namespace name, too.
it appears to be used within caching, and also within the FileSystem repo to implement the file watcher notifications, e.g.:
I don't have a specific suggestion on what it should be, but I don't think it can stay as is. Perhaps it goes to the main Caching namespace? (And we get rid of the unnecessary .Interfaces
namespace suffix as well.)
Action<XyzOptions>
I couldn't find in the new implementation of MemoryCache an UpdateCallback like the one in System.Runtime.Caching.MemoryCache.
http://referencesource.microsoft.com/#System.Runtime.Caching/System/Caching/MemoryCache.cs,124
I find this feature extremely useful for those scenarios where you want to repopulate the cache when it expires and you do not want the next request to pay the performance hit.
The cache is used to avoid repeated expensive operations. The main method for adding anything to the cache (Set) takes a callback factory to do said expensive work and configure the cache settings. This factory won't be called from GetOrAdd if the entry is already present.
Proposal: Because this object creation work can be expensive we should support doing it asynchronously. E.g. Task<object> SetAsync(string key, Func<ICacheAddContext, Task<object> factory);
Workaround: Today you can cache a Task<T>
, but that could also end up caching failure conditions.
https://github.com/aspnet/Caching/blob/dev/src/Microsoft.AspNet.MemoryCache/IMemoryCache.cs#L15
identifiing => identifying
"ASP.NET 5 in memory cache abstractions."
As there is now a SqlServerCache, this package can not be said as "in memory" abstraction.
Why should I use microsoft.framework.caching.redis (that has a dependency on stackexchange.redis) instead of just using the functionality already available in stackexchange.redis?
There is an implementation here: https://github.com/aspnet/Mvc/blob/dev/test/WebSites/HtmlGenerationWebSite/ISignalTokenProviderService.cs
I can copy it but I would suggest to add it OOB. The scenario is that multiple keys might depend on the same logical dependency, and this is a way to invalidate all of them. It's a very common used pattern in Orchard, so I assume it would be to with other applications using IMemoryCache
.
I understand it might also have been intentionally kept out too.
/cc @lodejard
We expect one very common use of the cache will be to cache files in memory until they are modified on disk. Create an expiration trigger that can monitor for file changes.
Also, consider adding some extensions that will read the file in for you and hook up this trigger by default.
string GetOrAddFileText(string key, string filename)
byte[] GetOrAddFileBytes(string key, string flename)
The IDistrbutedCache seems pretty basic at the moment compared to the IMemoryCache, as it only deals in byte arrays. Most developers using this interface will be writing some extensions methods for it.
This StackOverflow post is asking if these methods will eventually be part of ASP.NET Caching or if we should role our own.
The BinaryFormatter
is not available in .NET Core, is it going to be added in future? In the mean time the BinaryWriter
and BinaryReader
seem to be the way to go. I might just submit a pull request this evening.
See the StackOverflow post for the proposed extension methods.
I have the following error when trying to connect to networks from Mac OSX (Mono 4.2 ,dnx rc-1-final)
An unhandled exception has occurred: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING
StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING
at StackExchange.Redis.ConnectionMultiplexer.Connect (System.String configuration, System.IO.TextWriter log) <0x6ef60f0 + 0x00113> in :0
in Windows 10 I have no problem
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.