Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

SQLCache Set should allow a null timeout #209

Closed
iscifoni opened this issue Jun 27, 2016 · 4 comments
Closed

SQLCache Set should allow a null timeout #209

iscifoni opened this issue Jun 27, 2016 · 4 comments
Assignees

Comments

@iscifoni
Copy link

I have implemented sql cache:

using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.SqlServer;

.
.
.

public void ConfigureServices(IServiceCollection services)
 {
      services.AddSingleton<IDistributedCache>(serviceProvider =>
      new SqlServerCache(
            new CacheOptions( new SqlServerCacheOptions()
            {
                  ConnectionString = @"Data Source=localhost;Initial Catalog=CacheDB;Integrated Security=False;User Id=sa;Password=pippo",
                  SchemaName = "dbo",
                  TableName = "testCache"
            })
      ));
 }

public void Configure(IApplicationBuilder app,  IDistributedCache cache)
{
      var serverStartTimeString = DateTime.Now.ToString();
      byte[] val = Encoding.UTF8.GetBytes(serverStartTimeString);
      cache.Set("lastServerStartTime", val);

      app.Run(async (context) =>
      {
            context.Response.ContentType = "text/html";
            await context.Response.WriteAsync("Hello World!");
      });
}


but when I execute the code, I receive the error "Either absolute or sliding expiration needs to be provided.".
This because at line 33 of Microsoft.Extensions.Caching.Abstractions/DistributedCacheExtensions.cs the DistributedCacheEntryOptions parameter is a new value, with no setting for AbsoluteExpirationRelativeToNow or SlidingExpiration property.

If i use


cache.Set("lastServerStartTime", val, new DistributedCacheEntryOptions()
{
      AbsoluteExpirationRelativeToNow = new TimeSpan(1,0,0)
});

All works fine.

Need to modify Microsoft.Extensions.Caching.Abstractions/DistributedCacheExtensions.cs to pass default value forAbsoluteExpirationRelativeToNow or SlidingExpiration ??

@Tratcher
Copy link
Member

The Redis and DistributedMemory cache implementations allows this, I wonder why the SQL one doesn't. @kichalla?

@Tratcher Tratcher added the bug label Jun 27, 2016
@kichalla
Copy link
Member

@iscifoni Thanks for reporting this. This behavior is not ideal. For now you could probably create a new extension on IDistributedCache which sets these options.

@Tratcher Tratcher changed the title SQLCache set default value on DistributedCacheEntryOptions SQLCache Set should allow a null timeout Jun 28, 2016
@muratg muratg added this to the 1.1.0 milestone Jun 29, 2016
@kichalla
Copy link
Member

I am thinking that SQL Server cache should have a default sliding expiration for a cache entry, let's say 20 mins, if none specified by a user. These default timeout options would be exposed by SqlServerCacheOptions.

@iscifoni any thoughts on this?

@iscifoni
Copy link
Author

iscifoni commented Jul 19, 2016

For me the parameter DefaultSlidingExpiration on SqlServerCacheOptions class is what we need 😄 .

@Tratcher @kichalla

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants