تنظیمات کش توزیع شدهی مبتنی بر SQL Server در ASP.NET Core
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۱۰/۲۷ ۱۹:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
{
"dependencies": {
"Microsoft.Extensions.Caching.SqlServer": "1.1.0"
},
"tools": {
"Microsoft.Extensions.Caching.SqlConfig.Tools": "1.1.0-preview4-final"
}
} dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=sql_cache;Integrated Security=True;" "dbo" "AppSqlCache"
CREATE TABLE AppSqlCache ( Id NVARCHAR (449) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL, Value VARBINARY (MAX) NOT NULL, ExpiresAtTime DATETIMEOFFSET NOT NULL, SlidingExpirationInSeconds BIGINT NULL, AbsoluteExpiration DATETIMEOFFSET NULL, CONSTRAINT pk_Id PRIMARY KEY (Id) ); CREATE NONCLUSTERED INDEX Index_ExpiresAtTime ON AppSqlCache(ExpiresAtTime);
public class AppSqlCache
{
public string Id { get; set; }
public byte[] Value { get; set; }
public DateTimeOffset ExpiresAtTime { get; set; }
public long? SlidingExpirationInSeconds { get; set; }
public DateTimeOffset? AbsoluteExpiration { get; set; }
} public class MyDBDataContext : DbContext
{
public virtual DbSet<AppSqlCache> AppSqlCache { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AppSqlCache>(entity =>
{
entity.ToTable(name: "AppSqlCache", schema: "dbo");
entity.HasIndex(e => e.ExpiresAtTime).HasName("Index_ExpiresAtTime");
entity.Property(e => e.Id).HasMaxLength(449);
entity.Property(e => e.Value).IsRequired();
});
}
} public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=sql_cache;Integrated Security=True;";
options.SchemaName = "dbo";
options.TableName = "AppSqlCache";
}); public IActionResult Index()
{
HttpContext.Session.SetString("User", "VahidN");
return Json(true);
}
public IActionResult About()
{
var userContent = HttpContext.Session.GetString("User");
return Json(userContent);
}
services.Add(ServiceDescriptor.Singleton<IDistributedCache, SqlServerCache>());
using System;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
namespace Core1RtmEmptyTest.Controllers
{
public class CacheTestController : Controller
{
readonly IDistributedCache _cache;
public CacheTestController(IDistributedCache cache)
{
_cache = cache;
}
public IActionResult SetCacheData()
{
var time = DateTime.Now.ToLocalTime().ToString();
var cacheOptions = new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTime.Now.AddYears(1)
};
_cache.Set("Time", Encoding.UTF8.GetBytes(time), cacheOptions);
return View();
}
public IActionResult GetCacheData()
{
var time = Encoding.UTF8.GetString(_cache.Get("Time"));
ViewBag.data = time;
return View();
}
public bool RemoveCacheData()
{
_cache.Remove("Time");
return true;
}
}
} Value VARBINARY (MAX) NOT NULL,
public byte[] Value { get; set; }
<distributed-cache name="MyCacheItem2" expires-sliding="TimeSpan.FromMinutes(30)"> <p>From distributed-cache</p> @DateTime.Now.ToString() </distributed-cache>
SeverityCodeDescriptionProjectFileLineSuppression State ErrorPackage 'Microsoft.Extensions.Caching.SqlConfig.Tools 2.0.2' has a package type 'DotnetCliTool' that is not supported by project 'mvc-dsqlcache'.
dotnet sql-cache create "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=sql_cache;Integrated Security=True;" "dbo" "AppSqlCache"
dotnet : An error occurred. Cannot open database "sql_cache" requested by the login. The login failed.
At line:1 char:1
+ dotnet sql-cache create "Data Source=.;Initial Catalog=sql_cache;Inte ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (An error occurr...e login failed.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Login failed for user 'MicrosoftAccount\soleymani.meysam@hotmail.com'.