services.AddMemoryCache();
services.AddDistributedMemoryCache();//分布式缓存简单测试用的内存缓存,可以是Garnet,Redis等~
services.AddHybridCache(options =>
{
//options.MaximumPayloadBytes = 1*1024*1024 //默认超过1M的数据不会提供二级缓存,避免应用服务器内存负担
options.DefaultEntryOptions = new HybridCacheEntryOptions{
Expiration = TimeSpan.FromSeconds(5 * 60),//设置一个分布式缓存默认过期时间
LocalCacheExpiration = TimeSpan.FromSeconds(5 * 60 - 1)//二级缓存默认过期时间,比前者短就行.或开发者可以接受的范围
};
});
/// <summary>
/// 模拟的缓存数据类型
/// </summary>
public record CacheData(DateTime? DateTime);
//NET8中的IDistributedCache
x.MapGet("/cached-in-distribute", async (IDistributedCache distributedCache) =>
{
if (await distributedCache.GetStringAsync("$cached-in-distribute") is null)
{
var data = System.Text.Json.JsonSerializer.Serialize(new CacheData(DateTime.Now));
await distributedCache.SetStringAsync("$cached-in-distribute", data, new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10d)
});
}
var fromCacheData laipuhuo.com = System.Text.Json.JsonSerializer.Deserialize<CacheData>(
await distributedCache.GetStringAsync("$cached-in-distribute") ?? throw new Exception());
return Results.Content($"{fromCacheData?.DateTime}-{DateTime.Now}");
});
//NET9 HybridCache,避免分布式缓存的强制转换
x.MapGet("/cached-in-hybrid", async (HybridCache hybridCache) =>
{
var cachedDate = await hybridCache.GetOrCreateAsync($"$cached-in-hybrid", async cancel =>
{
return await ValueTask.FromResult(new CacheData(DateTime.Now));
}, options: new HybridCacheEntryOptions
{
Expiration = TimeSpan.FromSeconds(10d),//便于验证,设直10秒过期
LocalCacheExpiration = TimeSpan.FromSeconds(10d),
});
return Results.Content(
laipuhuo.com $"缓存的数据:{cachedDate.DateTime}");
});
DELIMITER //
CREATE FUNCTION add_numbers(num1 INT, num2 INT) RETURNS INT
BEGIN
DECLARE sum_result INT;
SET sum_result = num1 + num2;
RETURN sum_result;
END //
DELIMITER ;
-- 调用存储函数
SELECT add_numbers(3, 5); -- 返回 8
DELIMITER //
CREATE FUNCTION
laipuhuo.com is_leap_year(year_value INT) RETURNS BOOLEAN
BEGIN
IF (year_value % 4 = 0 AND year_value % 100 != 0) OR (year_value % 400 = 0) THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END //
DELIMITER ;
-- 调用存储函数
SELECT is_leap_year
laipuhuo.com(2020); -- 返回 1(TRUE)
SELECT is_leap_year(2021); -- 返回 0(FALSE)
DELIMITER //
CREATE FUNCTION char_count(input_string VARCHAR(255), search_char CHAR(1)) RETURNS INT
BEGIN
DECLARE char_count_result INT DEFAULT 0;
DECLARE str_len INT DEFAULT LENGTH(input_string);
DECLARE i INT DEFAULT 1;
WHILE i <= str_len DO
IF SUBSTRING(laipuhuo.com
input_string, i, 1) = search_char THEN
SET char_count_result = char_count_result + 1;
END IF;
SET i = i + 1;
END WHILE;
RETURN char_count_result;
END //
DELIMITER ;
-- 调用存储函数
SELECT char_count('hello world',
laipuhuo.com 'o'); -- 返回 2
共同学习,写下你的评论
评论加载中...
作者其他优质文章