Dev*_*nab 3 sql-server azure redis
应用程序性能是在关系数据库上使用缓存的主要原因之一。因为它以键值对的形式将数据存储在内存中,所以我们可以将经常访问的数据存储在缓存中,而这些数据的变化不是很频繁。从缓存中读取比数据库快得多。Redis 是分布式缓存市场中最好的解决方案之一。
我正在 Azure Redis 缓存和 Azure SQL Server 之间进行性能测试。我创建了一个简单的 ASP.NET Core 应用程序,并在其中多次从 SQL Server 数据库和 Redis 读取数据,并比较它们之间的读取持续时间。对于数据库读取,我使用了 Entity Framework Core,对于 Redis 读取,我使用了“Microsoft.Extensions.Caching.StackExchangeRedis”。
模型
using System;
namespace WebApplication2.Models
{
[Serializable]
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Subject { get; set; }
public Student()
{
Name = string.Empty;
Subject = string.Empty;
}
}
}
Run Code Online (Sandbox Code Playgroud)
实体框架核心数据上下文。
using Microsoft.EntityFrameworkCore;
using WebApplication2.Models;
namespace WebApplication2.Data
{
public class StudentContext : DbContext
{
public StudentContext(DbContextOptions<StudentContext> options)
: base(options)
{
}
public DbSet<Student>? Students { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
创业班
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
string studentDbConnectionString = Configuration.GetConnectionString("StudentDbConnectionString");
services.AddDbContext<StudentContext>(option => option.UseSqlServer(studentDbConnectionString));
string redisConnectionString = Configuration.GetConnectionString("RedisConnectionString");
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = redisConnectionString;
});
}
Run Code Online (Sandbox Code Playgroud)
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"StudentDbConnectionString": "[Azure SQL Server connection string]",
"RedisConnectionString": "[Azure Redis cache connection string]"
}
}
Run Code Online (Sandbox Code Playgroud)
家庭控制器
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using WebApplication2.Data;
using WebApplication2.Models;
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
private readonly StudentContext _studentContext;
private readonly IDistributedCache _cache;
public HomeController(StudentContext studentContext, IDistributedCache cache)
{
_studentContext = studentContext;
_cache = cache;
}
public IActionResult Index()
{
List<Student>? students = null;
var counter = 10000;
var sw = Stopwatch.StartNew();
for (var i = 0; i < counter; i++)
{
students = _studentContext.Students.OrderBy(student => student.Id).ToList();
}
sw.Stop();
ViewData["DatabaseDuraion"] = $"Database: {sw.ElapsedMilliseconds}";
if (students != null && students.Count > 0)
{
List<Student> studentsFromCache;
var key = "Students";
_cache.Set(key, ObjectToByteArray(students));
sw.Restart();
for (var i = 0; i < counter; i++)
{
studentsFromCache = (List<Student>)ByteArrayToObject(_cache.Get(key));
}
sw.Stop();
ViewData["RedisDuraion"] = $"Redis: {sw.ElapsedMilliseconds}";
}
return View();
}
private byte[] ObjectToByteArray(object obj)
{
var bf = new BinaryFormatter();
using var ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
private object ByteArrayToObject(byte[] arrBytes)
{
using var memStream = new MemoryStream();
var binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
object obj = binForm.Deserialize(memStream);
return obj;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Home\Index.cshtml 视图
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<p>@ViewData["DatabaseDuraion"]</p>
<p>@ViewData["RedisDuraion"]</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我发现 SQL Server 比 Redis 快。
ASP.NET Core 应用程序托管在 Azure 应用服务中,位置与 Azure SQL Server 和 Azure Redis 相同。
请让我知道为什么 Redis 比 SQL Server 慢?
我使用github.com/dotnet/BenchmarkDotNet对 Azure SQL Server 数据库和 Azure Redis 缓存进行了 10000 次读取的基准测试。SQL Server 数据库平均值:16.48 秒,Redis 平均值:29.53 秒。
我使用了 JMeter 并连接了 100 个用户,每个用户读取 SQL Server 数据库/Redis 1000 次。完成读取 SQL Server 数据库与 Redis 的总时间之间没有太大区别(两者都接近约 3 分 30 秒),但我看到 Azure SQL Server 数据库 DTU 上的负载。DTU 在测试期间接近 100%。
总之,我认为速度不是在 SQL Server 数据库上使用 Redis 缓存的唯一原因,另一个原因是 Redis 缓存减少了来自数据库的大量负载。
| 归档时间: |
|
| 查看次数: |
4675 次 |
| 最近记录: |