搜索

我们聊聊 C# 通用 Cache 缓冲类开发指南

发表于 2025-11-05 05:33:47 来源:全栈开发

在现代应用程序中,通用缓存是冲类一种有效的优化手段,能够显著提高系统的指南性能,减少延迟。通用缓存可以用于数据库查询结果的冲类存储、API 响应的指南缓存、复杂计算结果的通用保存等多个场景。本文将介绍如何开发一个通用的冲类 CacheService 类,并展示其在不同场景中的指南应用。

引言

缓存是通用一种存储机制,用于临时性地保存数据,冲类以减少数据获取的指南时间和频次。服务器托管在高性能应用程序中,通用合理使用缓存,冲类可以显著提高系统的指南响应速度,减轻后台服务的负担。接下来我们讲解如何开发一个通用的 CacheService 并展示其在具体场景中的应用。

开发通用的 CacheService

ICacheService 接口

首先,定义一个 ICacheService 接口,定义基本的缓存操作:

复制using System; using System.Threading.Tasks; public interface ICacheService { void Set<T>(string key, T value, TimeSpan expiration); T Get<T>(string key); Task SetAsync<T>(string key, T value, TimeSpan expiration); Task<T> GetAsync<T>(string key); void Remove(string key); Task RemoveAsync(string key); }1.2.3.4.5.6.7.8.9.10.11.12.13.

CacheService 实现

接下来,开发 CacheService 类,它实现了 ICacheService 接口。该类同时支持内存缓存和分布式缓存(例如 Redis),基于启动时的配置选择缓存方式:

复制using System; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; public class CacheService : ICacheService { private readonly IMemoryCache _memoryCache; private readonly IDistributedCache _distributedCache; private readonly ILogger<CacheService> _logger; private readonly bool _useDistributedCache; public CacheService(IMemoryCache memoryCache, IDistributedCache distributedCache, ILogger<CacheService> logger, bool useDistributedCache = false) { _memoryCache = memoryCache; _distributedCache = distributedCache; _logger = logger; _useDistributedCache = useDistributedCache; } public void Set<T>(string key, T value, TimeSpan expiration) { if (_useDistributedCache) { var options = new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = expiration }; var serializedValue = System.Text.Json.JsonSerializer.Serialize(value); _distributedCache.SetString(key, serializedValue, options); } else { var cacheEntryOptions = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = expiration }; _memoryCache.Set(key, value, cacheEntryOptions); } } public T Get<T>(string key) { if (_useDistributedCache) { var serializedValue = _distributedCache.GetString(key); if (serializedValue != null) { return System.Text.Json.JsonSerializer.Deserialize<T>(serializedValue); } return default; } _memoryCache.TryGetValue(key, out T value); return value; } public async Task SetAsync<T>(string key, T value, TimeSpan expiration) { if (_useDistributedCache) { var options = new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = expiration }; var serializedValue = System.Text.Json.JsonSerializer.Serialize(value); await _distributedCache.SetStringAsync(key, serializedValue, options); } else { var cacheEntryOptions = new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = expiration }; _memoryCache.Set(key, value, cacheEntryOptions); } } public async Task<T> GetAsync<T>(string key) { if (_useDistributedCache) { var serializedValue = await _distributedCache.GetStringAsync(key); if (serializedValue != null) { return System.Text.Json.JsonSerializer.Deserialize<T>(serializedValue); } return default; } _memoryCache.TryGetValue(key, out T value); return await Task.FromResult(value); } public void Remove(string key) { if (_useDistributedCache) { _distributedCache.Remove(key); } else { _memoryCache.Remove(key); } } public async Task RemoveAsync(string key) { if (_useDistributedCache) { await _distributedCache.RemoveAsync(key); } else { _memoryCache.Remove(key); } } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.

配置依赖注入

在项目的 Program.cs 中配置依赖注入:

复制using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; public class Program { static void Main(string[] args) { var serviceProvider = new ServiceCollection() .AddMemoryCache() .AddStackExchangeRedisCache(options => { options.Configuration = "localhost:6379"; }) .AddLogging() .AddSingleton<CacheService>() .BuildServiceProvider(); // 使用缓存服务的示例 var cacheService = serviceProvider.GetService<CacheService>(); cacheService.Set("key1", "value1",new TimeSpan(0, 10, 0)); var value = cacheService.Get<string>("key1"); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.

我们还可以启用分布试的云服务器缓冲(Redis);

复制static void Main(string[] args) { var serviceProvider = new ServiceCollection() .AddMemoryCache() .AddStackExchangeRedisCache(options => { options.Configuration = "localhost:6379"; }) .AddLogging() .AddSingleton<CacheService>(sp => { var memoryCache = sp.GetRequiredService<IMemoryCache>(); var distributedCache = sp.GetRequiredService<IDistributedCache>(); var logger = sp.GetRequiredService<ILogger<CacheService>>(); return new CacheService(memoryCache, distributedCache, logger, useDistributedCache: true); }) .BuildServiceProvider(); // 使用缓存服务的示例 var cacheService = serviceProvider.GetService<CacheService>(); cacheService.Set("key1", "value1",new TimeSpan(0, 10, 0)); var value = cacheService.Get<string>("key1"); }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.

图片

缓存应用场景示例

数据库查询结果缓存

DatabaseService.cs 复制using System; public class DatabaseService { private readonly ICacheService _cacheService; public DatabaseService(ICacheService cacheService) { _cacheService = cacheService; } public string GetDataFromDatabase(string query) { string cacheKey = $"DatabaseQuery-{query}"; var cacheData = _cacheService.Get<string>(cacheKey); if (cacheData != null) { return cacheData; } // 模拟数据库查询操作 string data = "返回数据"; _cacheService.Set(cacheKey, data, TimeSpan.FromMinutes(5)); return data; } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32. 程序入口 复制internal class Program { static void Main(string[] args) { var serviceProvider = new ServiceCollection() .AddMemoryCache() .AddStackExchangeRedisCache(options => { options.Configuration = "localhost:6379"; }) .AddLogging() .AddSingleton<CacheService>() .AddSingleton<ICacheService, CacheService>() .AddSingleton<DatabaseService>() .BuildServiceProvider(); var databaseService = serviceProvider.GetService<DatabaseService>(); string query = "SELECT * FROM Users"; Console.WriteLine("Query Result: " + databaseService.GetDataFromDatabase(query)); // 再次调用以展示缓存效果 Console.WriteLine("Query Result from Cache: " + databaseService.GetDataFromDatabase(query)); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.

图片

API 响应缓存

ApiService.cs 复制using System; using System.Net.Http; using System.Threading.Tasks; public class ApiService { private readonly ICacheService _cacheService; private readonly HttpClient _httpClient; public ApiService(ICacheService cacheService, HttpClient httpClient) { _cacheService = cacheService; _httpClient = httpClient; } public async Task<string> GetApiResponseAsync(string url) { string cacheKey = $"ApiUrl-{url}"; var cacheData = await _cacheService.GetAsync<string>(cacheKey); if (cacheData != null) { return cacheData; } var response = await _httpClient.GetStringAsync(url); await _cacheService.SetAsync(cacheKey, response, TimeSpan.FromMinutes(10)); return response; } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35. 程序入口 复制using System.Net.Http; using System.Threading.Tasks; public class Program { public static async Task Main(string[] args) { var serviceProvider = new ServiceCollection() .AddMemoryCache() .AddStackExchangeRedisCache(options => { options.Configuration = "localhost:6379"; }) .AddLogging() .AddSingleton<HttpClient>() .AddSingleton<CacheService>() .AddSingleton<ICacheService, CacheService>() .AddSingleton<ApiService>() .BuildServiceProvider(); var apiService = serviceProvider.GetService<ApiService>(); string apiUrl = "https://jsonplaceholder.typicode.com/posts"; var result = await apiService.GetApiResponseAsync(apiUrl); Console.WriteLine("API Response: " + result); // 再次调用以展示缓存效果 var cachedResult = await apiService.GetApiResponseAsync(apiUrl); Console.WriteLine("Cached API Response: " + cachedResult); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.

图片

复杂计算结果缓存

CalculationService.cs 复制using System; public class CalculationService { private readonly ICacheService _cacheService; public CalculationService(ICacheService cacheService) { _cacheService = cacheService; } public int HeavyComputation(int input) { string cacheKey = $"HeavyComputation-{input}"; var cacheData = _cacheService.Get<int>(cacheKey); if (cacheData != 0) { return cacheData; } // 模拟复杂计算 int result = input * input; _cacheService.Set(cacheKey, result, TimeSpan.FromMinutes(10)); return result; } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32. 程序入口 复制public class Program { public static void Main(string[] args) { var serviceProvider = new ServiceCollection() .AddMemoryCache() .AddStackExchangeRedisCache(options => { options.Configuration = "localhost:6379"; }) .AddLogging() .AddSingleton<CacheService>() .AddSingleton<ICacheService, CacheService>() .AddSingleton<CalculationService>() .BuildServiceProvider(); var calculationService = serviceProvider.GetService<CalculationService>(); int input = 42; Console.WriteLine("Computation Result: " + calculationService.HeavyComputation(input)); // 再次调用以展示缓存效果 Console.WriteLine("Cached Computation Result: " + calculationService.HeavyComputation(input)); } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.

总结

本文介绍了如何开发一个通用的 CacheService,并展示了其在不同场景中的应用:数据库查询结果缓存、API 响应缓存、复杂计算结果缓存。这种通用的缓存服务设计,可以显著提高应用的性能和响应速度,减少对外部资源(如数据库、外部 API)的频繁访问,从而优化用户体验。

通过上述示例,开发者可以更轻松地在项目中集成和管理缓存,提高应用的整体性能和可靠性。希望本文对你有所帮助,能够在实际项目中充分利用缓存技术。香港云服务器

随机为您推荐
版权声明:本站资源均来自互联网,如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

Copyright © 2016 Powered by 我们聊聊 C# 通用 Cache 缓冲类开发指南,全栈开发  滇ICP备2023006006号-32sitemap

回顶部