ef 仓储模式 Redis
接着写一下 上一章提到的
BaseRepositoryRedis.cs
先说说题外话:由于公司希望用到缓存来提高访问速度。那么我理所当然的想到redis。
这个无可厚非。可是当时我们的项目已经开发的差不多的了。。。。
我懒,也不可能到bll或者 dal去增加缓存代码。
于是我就想到了,既然我们的dal可以继承自BaseRepository.cs 那么一样也可以继承BaseRepositoryRedis.cs
只需要coder自己来选择是否需要缓存,需要的继承BaseRepositoryRedis,否则继承BaseRepository就好了!!!
说了这么多,那么放代码,
先创建一个RedisManager类(随便封装一下)
public class RedisManager<T> : BaseClass
{
RedisClient client = new RedisClient(RedisPath);
#region RedisHelper
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public virtual T Get<T>(string key)
{
var q = client.Get<T>(key);
return q;
}
/// <summary>
/// 设置
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <param name="expired"></param>
/// <returns></returns>
public virtual bool Set<T>(string key,T t,bool expired=true)
{
bool q=false;
if (expired)
{
q = client.Set(key, t,DateTime.Now.AddDays(ExpiredTime));
}
else
{
q = client.Set(key, t);
}
return q;
}
/// <summary>
/// 清空缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public virtual bool Remove(string key)
{
return client.Remove(key);
}
/// <summary>
/// 清空所有缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public virtual void FlushAll()
{
client.FlushAll();
}
/// <summary>
/// 获取所有key
/// </summary>
public virtual List<string> GetAllKeys()
{
var q = client.GetAllKeys();
return q;
}
/// <summary>
/// 获取所有键值对
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
public virtual IDictionary<string, T> GetAll(List<string> keys)
{
IDictionary<string, T> dict = client.GetAll<T>(keys);
return dict;
}
#endregion
}
然后创建接口
IDAL
namespace IDAL
{
public interface IBaseRepositoryRedis<T>
{
/// <summary>
/// 添加
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>添加后的数据实体</returns>
T Add(T entity, bool hasRedis = true);
/// <summary>
/// 添加
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>添加后的数据实体</returns>
bool AddOK(T entity, bool hasRedis = true);
/// <summary>
/// 查询记录数
/// </summary>
/// <param name="predicate">条件表达式</param>
/// <returns>记录数</returns>
int Count(Expression<Func<T, bool>> predicate, bool hasRedis = true);
/// <summary>
/// 更新
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>是否成功</returns>
T Update(T entity, bool hasRedis = true);
/// <summary>
/// 更新
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>是否成功</returns>
bool UpdateOK(T entity, bool hasRedis = true);
/// <summary>
/// 删除
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>是否成功</returns>
bool Delete(T entity, bool hasRedis = true);
/// <summary>
/// 是否存在
/// </summary>
/// <param name="anyLambda">查询表达式</param>
/// <returns>布尔值</returns>
bool Exist(Expression<Func<T, bool>> anyLambda, bool hasRedis = true);
/// <summary>
/// 查询数据
/// </summary>
/// <param name="whereLambda">查询表达式</param>
/// <returns>实体</returns>
T Find(Expression<Func<T, bool>> whereLambda, bool hasRedis = true);
/// <summary>
/// 查找数据列表
/// </summary>
/// <typeparam name="S">排序</typeparam>
/// <param name="whereLamdba">查询表达式</param>
/// <returns></returns>
List<T> FindList(Expression<Func<T, bool>> whereLamdba, bool hasRedis = true);
/// <summary>
/// 查找数据列表
/// </summary>
/// <typeparam name="S">排序</typeparam>
/// <param name="whereLamdba">查询表达式</param>
/// <param name="isAsc">是否升序</param>
/// <param name="orderLamdba">排序表达式</param>
/// <returns></returns>
List<T> FindList<S>(Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true);
/// <summary>
/// 查找分页数据列表
/// </summary>
/// <typeparam name="S">排序</typeparam>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="totalRecord">总记录数</param>
/// <param name="whereLamdba">查询表达式</param>
/// <param name="isAsc">是否升序</param>
/// <param name="orderLamdba">排序表达式</param>
/// <returns></returns>
List<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true);
List<T> FindListBySQL<T>(string sql, params object[] parameters);
int ExecuteBySQL(string sql, params object[] parameters);
}
}
IBLL
namespace IBLL
{
public interface IBaseRepositoryRedisBLL<T> where T : class
{
/// <summary>
/// 添加
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>添加后的数据实体</returns>
T Add(T entity, bool hasRedis = true);
/// <summary>
/// 添加
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>添加后的数据实体</returns>
bool AddOK(T entity, bool hasRedis = true);
/// <summary>
/// 查询记录数
/// </summary>
/// <param name="predicate">条件表达式</param>
/// <returns>记录数</returns>
int Count(Expression<Func<T, bool>> predicate, bool hasRedis = true);
/// <summary>
/// 更新
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>是否成功</returns>
T Update(T entity, bool hasRedis = true);
/// <summary>
/// 更新
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>是否成功</returns>
bool UpdateOK(T entity, bool hasRedis = true);
/// <summary>
/// 删除
/// </summary>
/// <param name="entity">数据实体</param>
/// <returns>是否成功</returns>
bool Delete(T entity, bool hasRedis = true);
/// <summary>
/// 是否存在
/// </summary>
/// <param name="anyLambda">查询表达式</param>
/// <returns>布尔值</returns>
bool Exist(Expression<Func<T, bool>> anyLambda, bool hasRedis = true);
/// <summary>
/// 查询数据
/// </summary>
/// <param name="whereLambda">查询表达式</param>
/// <returns>实体</returns>
T Find(Expression<Func<T, bool>> whereLambda, bool hasRedis = true);
/// <summary>
/// 查找数据列表
/// </summary>
/// <typeparam name="S">排序</typeparam>
/// <param name="whereLamdba">查询表达式</param>
/// <returns></returns>
List<T> FindList(Expression<Func<T, bool>> whereLamdba, bool hasRedis = true);
/// <summary>
/// 查找数据列表
/// </summary>
/// <typeparam name="S">排序</typeparam>
/// <param name="whereLamdba">查询表达式</param>
/// <param name="isAsc">是否升序</param>
/// <param name="orderLamdba">排序表达式</param>
/// <returns></returns>
List<T> FindList<S>(Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true);
/// <summary>
/// 查找分页数据列表
/// </summary>
/// <typeparam name="S">排序</typeparam>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">每页记录数</param>
/// <param name="totalRecord">总记录数</param>
/// <param name="whereLamdba">查询表达式</param>
/// <param name="isAsc">是否升序</param>
/// <param name="orderLamdba">排序表达式</param>
/// <returns></returns>
List<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true);
List<T> FindListBySQL<T>(string sql, params object[] parameters);
int ExecuteBySQL(string sql, params object[] parameters);
}
}
DAL
这里,我们创建 BaseRepositoryRedis
namespace DAL.Base
{
public class BaseRepositoryRedis<T> : BaseClass, IBaseRepositoryRedis<T> where T : class
{
public ELDBEntity dbEF = DbContextFactory.GetCurrentContext();
RedisBLL<T> redis = new RedisBLL<T>();
public virtual T Add(T entity,bool hasRedis=true)
{
dbEF.Entry<T>(entity).State = EntityState.Added;
dbEF.SaveChanges();
if (hasRedis)
{
var q = SaveRedis();
}
return entity;
}
public virtual bool AddOK(T entity, bool hasRedis = true)
{
dbEF.Entry<T>(entity).State = EntityState.Added;
int count = dbEF.SaveChanges();
if (hasRedis)
{
var q = SaveRedis();
}
return count > 0;
}
public virtual int Count(Expression<Func<T, bool>> predicate, bool hasRedis = true)
{
List<T> q = new List<T>();
if (hasRedis)
{
var s = redis.Get<string>(typeof(T).ToString());
if (string.IsNullOrEmpty(s))
{
q = SaveRedis();
}
else
{
q = JsonConvert.DeserializeObject<List<T>>(s);
}
return q.Count(predicate.Compile());
}
return dbEF.Set<T>().AsNoTracking().Count(predicate);
}
public virtual T Update(T entity, bool hasRedis = true)
{
dbEF.Set<T>().Attach(entity);
dbEF.Entry<T>(entity).State = EntityState.Modified;
dbEF.SaveChanges();
if (hasRedis)
{
var q = SaveRedis();
}
return entity;
}
public virtual bool UpdateOK(T entity, bool hasRedis = true)
{
dbEF.Set<T>().Attach(entity);
dbEF.Entry<T>(entity).State = EntityState.Modified;
int count = dbEF.SaveChanges();
if (hasRedis)
{
var q = SaveRedis();
}
return count>0;
}
public virtual bool Delete(T entity, bool hasRedis = true)
{
dbEF.Set<T>().Attach(entity);
dbEF.Entry<T>(entity).State = EntityState.Deleted;
int count = dbEF.SaveChanges();
if (hasRedis)
{
var q = SaveRedis();
}
return count > 0;
}
public virtual bool Exist(Expression<Func<T, bool>> anyLambda, bool hasRedis = true)
{
List<T> q = new List<T>();
if (hasRedis)
{
var s = redis.Get<string>(typeof(T).ToString());
if (string.IsNullOrEmpty(s))
{
q = SaveRedis();
}
else
{
q = JsonConvert.DeserializeObject<List<T>>(s);
}
return q.Exists(anyLambda.Compile().ToPredicate());
}
return dbEF.Set<T>().AsNoTracking().Any(anyLambda);
}
public virtual T Find(Expression<Func<T, bool>> whereLambda, bool hasRedis = true)
{
List<T> q = new List<T>();
if (hasRedis)
{
var s = redis.Get<string>(typeof(T).ToString());
if (string.IsNullOrEmpty(s))
{
q = SaveRedis();
}
else
{
q = JsonConvert.DeserializeObject<List<T>>(s);
}
return q.Find(whereLambda.Compile().ToPredicate());
}
T _entity = dbEF.Set<T>().AsNoTracking().FirstOrDefault<T>(whereLambda);
return _entity;
}
public virtual List<T> FindList(Expression<Func<T, bool>> whereLamdba, bool hasRedis = true)
{
List<T> q = new List<T>();
if (hasRedis)
{
var s = redis.Get<string>(typeof(T).ToString());
if (string.IsNullOrEmpty(s))
{
q = SaveRedis();
}
else
{
q = JsonConvert.DeserializeObject<List<T>>(s);
}
}
else
{
q = dbEF.Set<T>().AsNoTracking().Where<T>(whereLamdba).ToList();
}
return q.FindAll(whereLamdba.Compile().ToPredicate());;
}
public virtual List<T> FindList<S>(Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true)
{
List<T> q = new List<T>();
if (hasRedis)
{
var s = redis.Get<string>(typeof(T).ToString());
if (string.IsNullOrEmpty(s))
{
q = SaveRedis();
}
else
{
q = JsonConvert.DeserializeObject<List<T>>(s);
}
q = q.FindAll(whereLamdba.Compile().ToPredicate());
}
else
{
q=dbEF.Set<T>().AsNoTracking().Where<T>(whereLamdba).ToList();
}
if (isAsc) q = q.OrderBy<T, S>(orderLamdba.Compile()).ToList();
else q = q.OrderByDescending<T, S>(orderLamdba.Compile()).ToList();
return q;
}
public virtual List<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true)
{
List<T> q = new List<T>();
if (hasRedis)
{
var s = redis.Get<string>(typeof(T).ToString());
if (string.IsNullOrEmpty(s))
{
q = SaveRedis();
}
else
{
q = JsonConvert.DeserializeObject<List<T>>(s);
}
q = q.FindAll(whereLamdba.Compile().ToPredicate());
}
else
{
q = dbEF.Set<T>().AsNoTracking().Where<T>(whereLamdba).ToList();
}
totalRecord = q.Count();
if (isAsc) q = q.OrderBy<T, S>(orderLamdba.Compile()).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize).ToList();
else q = q.OrderByDescending<T, S>(orderLamdba.Compile()).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize).ToList();
return q;
}
public virtual PagedList<T> FindPageList1<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true)
{
List<T> q = new List<T>();
if (hasRedis)
{
var s = redis.Get<string>(typeof(T).ToString());
if (string.IsNullOrEmpty(s))
{
q = SaveRedis();
}
else
{
q = JsonConvert.DeserializeObject<List<T>>(s);
}
q = q.FindAll(whereLamdba.Compile().ToPredicate());
}
else
{
q = dbEF.Set<T>().AsNoTracking().Where<T>(whereLamdba).ToList();
}
totalRecord = q.Count();
PagedList<T> resultList = null;
if (isAsc) resultList = q.OrderBy<T, S>(orderLamdba.Compile()).ToPagedList(pageIndex, pageSize);
else resultList = q.OrderByDescending<T, S>(orderLamdba.Compile()).ToPagedList(pageIndex, pageSize);
return resultList;
}
public virtual List<T> FindListBySQL<T>(string sql, params object[] parameters)
{
var list = dbEF.Database.SqlQuery<T>(sql, parameters).ToList();
return list;
}
public virtual int ExecuteBySQL(string sql, params object[] parameters)
{
var q = dbEF.Database.ExecuteSqlCommand(sql, parameters);
return q;
}
private List<T> SaveRedis()
{
List<T> q = dbEF.Set<T>().AsNoTracking().ToList();
var json = JsonConvert.SerializeObject(q);
redis.Set<string>(typeof(T).Name, json);
return q;
}
}
}
PS:说一下,为何要用json存储? 其实我也很无奈(Redis 无法保存ef复杂对象),看链接:http://blog.csdn.net/hanjun0612/article/details/54409892
BLL
namespace BLL.Base
{
public class BaseRepositoryRedisBLL<T> : BaseClass, IBaseRepositoryRedisBLL<T> where T : class
{
BaseRepositoryRedis<T> obj = new BaseRepositoryRedis<T>();
public virtual T Add(T entity, bool hasRedis = true)
{
return obj.Add(entity);
}
public virtual bool AddOK(T entity, bool hasRedis = true)
{
return obj.AddOK(entity);
}
public virtual int Count(System.Linq.Expressions.Expression<Func<T, bool>> predicate, bool hasRedis = true)
{
return obj.Count(predicate);
}
public virtual T Update(T entity, bool hasRedis = true)
{
return obj.Update(entity);
}
public virtual bool UpdateOK(T entity, bool hasRedis = true)
{
return obj.UpdateOK(entity);
}
public virtual bool Delete(T entity, bool hasRedis = true)
{
return obj.Delete(entity);
}
public virtual bool Exist(System.Linq.Expressions.Expression<Func<T, bool>> anyLambda, bool hasRedis = true)
{
return obj.Exist(anyLambda);
}
public virtual T Find(Expression<Func<T, bool>> whereLambda, bool hasRedis = true)
{
return obj.Find(whereLambda);
}
public virtual List<T> FindList(Expression<Func<T, bool>> whereLamdba, bool hasRedis = true)
{
var _list = obj.FindList(whereLamdba);
return _list;
}
public virtual List<T> FindList<S>(Expression<Func<T, bool>> whereLamdba, bool isAsc, System.Linq.Expressions.Expression<Func<T, S>> orderLamdba, bool hasRedis = true)
{
return obj.FindList<S>(whereLamdba, isAsc, orderLamdba);
}
public virtual List<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true)
{
return obj.FindPageList<S>(pageIndex, pageSize, out totalRecord, whereLamdba, isAsc, orderLamdba);
}
public virtual PagedList<T> FindPageList1<S>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLamdba, bool hasRedis = true)
{
return obj.FindPageList1<S>(pageIndex, pageSize, out totalRecord, whereLamdba, isAsc, orderLamdba);
}
public virtual List<T> FindListBySQL<T>(string sql, params object[] parameters)
{
return obj.FindListBySQL<T>(sql, parameters);
}
public virtual int ExecuteBySQL(string sql, params object[] parameters)
{
return obj.ExecuteBySQL(sql, parameters);
}
}
}
最后,看一下用法
public class TestDAL : BaseRepositoryRedis<SYS_User>
{
//创建自定义方法
public string HelloWorld(string name)
{
return name + " Hello";
}
}
BLL
namespace BLL.Test
{
//继承BaseRepositoryBLL,拥有常用方法
public class TestBLL : BaseRepositoryRedisBLL<SYS_User>
{
}
}
ef 仓储模式 Redis的更多相关文章
- ef 仓储模式
构建一个仓储模式. Model 大家自己创建就行了,上个图,就不多说了(我是code first) IDAL namespace IDAL { public interface IBaseReposi ...
- MVC+EF 理解和实现仓储模式和工作单元模式
MVC+EF 理解和实现仓储模式和工作单元模式 原文:Understanding Repository and Unit of Work Pattern and Implementing Generi ...
- 用MVC5+EF6+WebApi 做一个考试功能(六) 仓储模式 打造EF通用仓储类
前言 年底工作比较忙,年度总结还没写,项目要上线,回老家过年各种准备.尤其是给长辈给侄子侄女准备礼物头都大了. 原来想年前先出一版能用的,我看有点悬了,尽量先把大体功能弄出来,扔掉一些,保证能考试,然 ...
- 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- 4.在MVC中使用仓储模式进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...
- 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- ASP.NET Mvc实用框架(一)Ioc、仓储模式和单元工作模式
Framework.EF 首先看一下这个类库: Extended文件夹存放的是EntityFramework.Extensions这个插件的源代码,没有别的原因,就是本人觉得这个插件挺好的,每次省的下 ...
- MVC5+EF6 入门完整教程十一:细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
- MVC5+EF6 入门完整教程11--细说MVC中仓储模式的应用
摘要: 第一阶段1~10篇已经覆盖了MVC开发必要的基本知识. 第二阶段11-20篇将会侧重于专题的讲解,一篇文章解决一个实际问题. 根据园友的反馈, 本篇文章将会先对呼声最高的仓储模式进行讲解. 文 ...
随机推荐
- Android ScrollView和ListView联用,且ListView可以下拉刷新和上拉加载
ScrollView嵌套listView且ListView可以实现上拉加载. 由于代码太长,在此只提供实现思路: 先不说上拉加载的事,咱们先回想一下,ScrollView和LsitView联用,时的解 ...
- oracle pls-00382:表达式类型错误
转载至:pls-00382:表达式类型错误 错误:pls-00382:表达式类型错误 如何产生: 我是在将一个动态sql付给一个nvarchar2变量是出现这个错误的,示例代码如下: declare ...
- Python、Lua和Ruby三大语言脚本哪家强?
这三种语言中的哪一种更合适你的项目?很大程度取决于你想达到什么样的目标.作为一书的结尾,在这一节里,我会描述一下它们各自的优点和不足. Python的优点和不足 Python的优点如下: 1. P ...
- DNS 协议
DNS 入门 域名系统(英文:Domain Name System,缩写:DNS)是互联网的一项服务.它作为将域名和 IP 地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.DNS 使用 T ...
- LOJ2537 PKUWC2018 Minimax 树形DP、线段树合并
传送门 题意:自己去看 首先可以知道,每一个点都有几率被选到,所以$i$与$V_i$的关系是确定了的. 所以我们只需要考虑每一个值的取到的概率. 很容易设计出一个$DP$:设$f_{i,j}$为在第$ ...
- angularjs的ng-change事件演示
今天练习angularjs的ng-change事件. 比如用户作出选择时,系统所指定的选项中,没有用户合适的选项.此时我们可以让用户填写. 刚开始文本框是隐藏的.当用户选择了checkbox之后才会显 ...
- 【php增删改查实例】第二十三节 - PHP文件上传
22. PHP文件上传 22.1 资源文件 将这三个东西拷贝项目的根目录. 拷贝完毕后,打开upload.html: 现在,我们在项目的根目录去编写一个upload.php. PHP给我们提供了很多关 ...
- ActiveMQ在C#中的应用
本文是在.NET Framework框架下的应用,截止到目前ActiveMQ还不支持.NET Core,而RabbitMQ已经支持.NET Core,希望ActiveMQ能尽快支持. ActiveMQ ...
- Linux Namespace : IPC
IPC namespace 用来隔离 System V IPC 对象和 POSIX message queues.其中 System V IPC 对象包含共享内存.信号量和消息队列,笔者在<Sy ...
- Linux下的Mongodb部署应用梳理
一.Mongodb简介 官网地址:http://www.mongodb.org/ MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.MongoDB 是一 ...