Redis缓存+Dapper,API方法

本文介绍如何结合Dapper数据访问层(DAL)与Redis缓存来优化API方法。首先展示了使用Dapper的简单DAL层方法,通过SqlConnection连接数据库并执行SQL查询获取数据。接着,讨论了如何在WebAPI控制器中利用Redis缓存,通过RedisHelper对Get请求的API方法进行缓存,提高数据获取效率。同时,还包括了一个添加数据的POST方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

1、Dapper DAL层方法

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Model;
namespace DAL
{
public class UserDAL
{
//显示
public List Show()
{
using (SqlConnection conn = new SqlConnection(“Data Source=.;Initial Catalog=Day18-7-6;Integrated Security=True”))
{
return conn.Query(“select * from Yhxx”).ToList();
}

    }
    //添加
    public int Add(YhxxModel s)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Day18-7-6;Integrated Security=True"))
        {
            return conn.Execute(string.Format("insert into Yhxx val
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值