EF封装类 增加版,增加从缓存中查找数据方法,供参考!

简介:

EF封装类 增加版,增加从缓存中查找数据方法,供参考!

这个类是抽象类,我这里增加了需要子类验证的方法ValidateEntity,方便扩展,若想直接使用该类,可以将该类更改成静态类,里面所有的方法都改成静态方法就可以直接调用了,可能有不足之处,欢迎大家在本文下面评论留言,共同完善,谢谢!

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Data.Objects.DataClasses;
using  ZBService.Model;
using  System.Linq.Expressions;
using  System.Web;
 
namespace  ZBService
{
     public  abstract  class  ServiceBase<T> where  T : EntityObject
     {
         /// <summary>
         /// EF上下文对象
         /// </summary>
         protected  mZhaoBiaoEntities zbEntities = new  mZhaoBiaoEntities();
 
         /// <summary>
         /// 判断是否存在
         /// </summary>
         /// <param name="whereExpr"></param>
         /// <returns></returns>
         public  bool  Exist(Expression<Func<T, bool >> whereExpr)
         {
             return  ( this .Count(whereExpr) > 0);
         }
 
         /// <summary>
         /// 获取记录数
         /// </summary>
         /// <param name="whereExpr"></param>
         /// <returns></returns>
         public  int  Count(Expression<Func<T, bool >> whereExpr)
         {
             return  zbEntities.CreateObjectSet<T>().Where(whereExpr).Count();
         }
 
         /// <summary>
         /// 查找实体对象
         /// </summary>
         /// <param name="whereExpr"></param>
         /// <returns></returns>
         public  T Find(Expression<Func<T, bool >> whereExpr)
         {
             return  zbEntities.CreateObjectSet<T>().Where(whereExpr).FirstOrDefault();
         }
 
         /// <summary>
         /// 从缓存中查找实体对象
         /// </summary>
         /// <param name="whereExpr"></param>
         /// <param name="cacheKey"></param>
         /// <param name="expireTime"></param>
         /// <returns></returns>
         public  T FindFromCache(Expression<Func<T, bool >> whereExpr, string  cacheKey, DateTime expireTime)
         {
             T entity;
             object  obj = HttpRuntime.Cache[cacheKey];
             if  (obj == null )
             {
                 entity = this .Find(whereExpr);
                 HttpRuntime.Cache.Insert(cacheKey, entity, null , expireTime, System.Web.Caching.Cache.NoSlidingExpiration);
             }
             else
             {
                 entity = obj as  T;
             }
             return  entity;
         }
 
         /// <summary>
         /// 查找实体对象列表
         /// </summary>
         /// <param name="whereExpr"></param>
         /// <returns></returns>
         public  IEnumerable<T> FindList<TKey>(Expression<Func<T, bool >> whereExpr, Expression<Func<T, TKey>> orderbyExpr, int  orderDirection)
         {
             return  this .FindList<T, TKey>(whereExpr, t => t, orderbyExpr, orderDirection);
         }
 
         /// <summary>
         /// 从缓存中查找实体对象列表
         /// </summary>
         /// <typeparam name="TKey"></typeparam>
         /// <param name="whereExpr"></param>
         /// <param name="orderbyExpr"></param>
         /// <param name="orderDirection"></param>
         /// <param name="cacheKey"></param>
         /// <param name="expireTime"></param>
         /// <returns></returns>
         public  IEnumerable<T> FindListFromCache<TKey>(Expression<Func<T, bool >> whereExpr, Expression<Func<T, TKey>> orderbyExpr, int  orderDirection, string  cacheKey, DateTime expireTime)
         {
             return  this .FindListFromCache(whereExpr, t => t, orderbyExpr, orderDirection, -1, cacheKey, expireTime);
         }
 
         /// <summary>
         /// 查找实体对象列表
         /// </summary>
         /// <typeparam name="TResult"></typeparam>
         /// <typeparam name="TKey"></typeparam>
         /// <param name="whereExpr"></param>
         /// <param name="selectExpr"></param>
         /// <param name="orderbyExpr"></param>
         /// <param name="orderDirection"></param>
         /// <param name="returnCount"></param>
         /// <returns></returns>
         public  IEnumerable<TResult> FindList<TResult, TKey>(Expression<Func<T, bool >> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int  orderDirection, int  returnCount = -1)
         {
             var  result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
             if  (result != null  && result.Count() > 0)
             {
                 if  (orderDirection > 0)
                 {
                     result = result.OrderByDescending(orderbyExpr);
                 }
                 else
                 {
                     result = result.OrderBy(orderbyExpr);
                 }
 
                 if  (returnCount > 0)
                 {
                     result = result.Take(returnCount);
                 }
 
                 return  result.ToList();
             }
             return  null ;
         }
 
         /// <summary>
         /// 从缓存中查找对象列表
         /// </summary>
         /// <typeparam name="TResult"></typeparam>
         /// <typeparam name="TKey"></typeparam>
         /// <param name="whereExpr"></param>
         /// <param name="selectExpr"></param>
         /// <param name="orderbyExpr"></param>
         /// <param name="orderDirection"></param>
         /// <param name="returnCount"></param>
         /// <param name="cacheKey"></param>
         /// <param name="expireTime"></param>
         /// <returns></returns>
         public  IEnumerable<TResult> FindListFromCache<TResult, TKey>(Expression<Func<T, bool >> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int  orderDirection,
                                     int  returnCount, string  cacheKey, DateTime expireTime)
         {
             IEnumerable<TResult> resultList;
             object  obj = HttpRuntime.Cache[cacheKey];
             if  (obj == null )
             {
                 resultList = this .FindList(whereExpr, selectExpr,orderbyExpr, orderDirection,returnCount);
                 HttpRuntime.Cache.Insert(cacheKey, resultList, null , expireTime, System.Web.Caching.Cache.NoSlidingExpiration);
             }
             else
             {
                 resultList = obj as  IEnumerable<TResult>;
             }
             return  resultList;
         }
 
         /// <summary>
         /// 分页查找实体对象列表
         /// </summary>
         /// <typeparam name="TResult"></typeparam>
         /// <typeparam name="TKey"></typeparam>
         /// <param name="whereExpr"></param>
         /// <param name="selectExpr"></param>
         /// <param name="orderbyExpr"></param>
         /// <param name="orderDirection"></param>
         /// <param name="pageSize"></param>
         /// <param name="pageNo"></param>
         /// <param name="recordCount"></param>
         /// <returns></returns>
         public  IEnumerable<TResult> FindListByPage<TResult, TKey>(Expression<Func<T, bool >> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int  orderDirection, int  pageSize, int  pageNo, out  int  recordCount)
         {
             recordCount = 0;
             var  result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
             if  (result == null ) return  null ;
             recordCount = result.Count();
 
             if  (pageNo > recordCount) pageNo = recordCount;
             if  (pageNo <= 0) pageNo = 1;
 
             if  (recordCount > 0)
             {
                 if  (recordCount > pageSize)
                 {
                     if  (orderDirection > 0)
                     {
                         return  result.OrderByDescending(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
                     }
                     else
                     {
                         return  result.OrderBy(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
                     }
                 }
                 else
                 {
                     if  (orderDirection > 0)
                     {
                         return  result.OrderByDescending(orderbyExpr).ToList();
                     }
                     else
                     {
                         return  result.OrderBy(orderbyExpr).ToList();
                     }
                 }
 
             }
             return  null ;
         }
 
         /// <summary>
         /// 从缓存中分页查找实体对象列表
         /// </summary>
         /// <typeparam name="TResult"></typeparam>
         /// <typeparam name="TKey"></typeparam>
         /// <param name="whereExpr"></param>
         /// <param name="selectExpr"></param>
         /// <param name="orderbyExpr"></param>
         /// <param name="orderDirection"></param>
         /// <param name="pageSize"></param>
         /// <param name="pageNo"></param>
         /// <param name="recordCount"></param>
         /// <param name="cacheKey"></param>
         /// <param name="expireTime"></param>
         /// <returns></returns>
         public  IEnumerable<TResult> FindListByPageFromCache<TResult, TKey>(Expression<Func<T, bool >> whereExpr, Expression<Func<T, TResult>> selectExpr, Expression<Func<TResult, TKey>> orderbyExpr, int  orderDirection, int  pageSize, int  pageNo, out  int  recordCount,
                     string  cacheKey, DateTime expireTime)
         {
             recordCount = 0;
             IQueryable<TResult> result;
             object  obj = HttpRuntime.Cache[cacheKey];
             if  (obj == null )
             {
                 result = zbEntities.CreateObjectSet<T>().Where(whereExpr).Select(selectExpr);
             }
             else
             {
                 result = obj as  IQueryable<TResult>;
             }
 
             if  (result == null ) return  null ;
             recordCount = result.Count();
 
 
             if  (pageNo > recordCount) pageNo = recordCount;
             if  (pageNo <= 0) pageNo = 1;
 
             if  (recordCount > 0)
             {
                 if  (recordCount > pageSize)
                 {
                     if  (orderDirection > 0)
                     {
                         return  result.OrderByDescending(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
                     }
                     else
                     {
                         return  result.OrderBy(orderbyExpr).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
                     }
                 }
                 else
                 {
                     if  (orderDirection > 0)
                     {
                         return  result.OrderByDescending(orderbyExpr).ToList();
                     }
                     else
                     {
                         return  result.OrderBy(orderbyExpr).ToList();
                     }
                 }
 
             }
             return  null ;
 
         }
 
         /// <summary>
         /// 增加实体
         /// </summary>
         /// <param name="entity"></param>
         public  virtual  void  Add(T entity)
         {
             this .ValidateEntity(entity, ValidateMode.Add);
             zbEntities.CreateObjectSet<T>().AddObject(entity);
         }
 
 
         /// <summary>
         /// 增加实体列表
         /// </summary>
         /// <param name="entities"></param>
         public  virtual  void  AddList(IEnumerable<T> entities)
         {
             var  objSet = zbEntities.CreateObjectSet<T>();
             foreach  (T entity in  entities)
             {
                 this .ValidateEntity(entity, ValidateMode.Add);
                 objSet.AddObject(entity);
             }
         }
 
         /// <summary>
         /// 更新已分离实体,若未分离则不需要执行该方法
         /// </summary>
         /// <param name="entity"></param>
         public  virtual  void  Update(T entity)
         {
             this .ValidateEntity(entity, ValidateMode.Update);
             zbEntities.CreateObjectSet<T>().ApplyCurrentValues(entity);
         }
 
         /// <summary>
         /// 删除实体
         /// </summary>
         /// <param name="entity"></param>
         public  virtual  void  Delete(T entity)
         {
             this .ValidateEntity(entity, ValidateMode.Delete);
             zbEntities.CreateObjectSet<T>().DeleteObject(entity);
         }
 
         /// <summary>
         /// 删除实体
         /// </summary>
         /// <param name="whereExpr"></param>
         public  virtual  void  Delete(Expression<Func<T, bool >> whereExpr)
         {
             var  objSet = zbEntities.CreateObjectSet<T>();
             T entity = objSet.Where(whereExpr).Single();
             //this.ValidateEntity(entity, ValidateMode.Delete);
             objSet.DeleteObject(entity);
         }
 
         /// <summary>
         /// 删除实体列表
         /// </summary>
         /// <param name="entities"></param>
         public  virtual  void  DeleteList(IEnumerable<T> entities)
         {
             var  objSet = zbEntities.CreateObjectSet<T>();
             foreach  (T entity in  entities)
             {
                 //this.ValidateEntity(entity, ValidateMode.Delete);
                 objSet.DeleteObject(entity);
             }
         }
 
 
         /// <summary>
         /// 提交保存所有变更操作
         /// </summary>
         public  void  SubmitSave()
         {
             zbEntities.SaveChanges();
         }
 
 
         /// <summary>
         /// 验证
         /// </summary>
         /// <param name="entity"></param>
         /// <returns></returns>
         protected  virtual  void  ValidateEntity(T entity, ValidateMode mode = ValidateMode.Add)
         {
 
         }
 
         /// <summary>
         /// 验证模式
         /// </summary>
         protected  enum  ValidateMode
         {
             Add = 0,
             Update = 1,
             Delete = -1
         }
 
     }
}

之前发表的文章详见:http://www.cnblogs.com/zuowj/p/4259515.html

本文转自 梦在旅途 博客园博客,原文链接:http://www.cnblogs.com/zuowj/p/4278537.html  ,如需转载请自行联系原作者

相关文章
|
2月前
|
Web App开发 存储 缓存
如何精准清除特定类型或标签的缓存数据?
如何精准清除特定类型或标签的缓存数据?
268 57
|
4月前
|
缓存 NoSQL 关系型数据库
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
|
26天前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
360 10
|
1月前
|
缓存 监控 Ubuntu
Ubuntu操作系统下清除系统缓存与无用文件的方法
通过上述步骤断行综合性地对Ubuntu进行优化与整洁可显著改善其性能表现及响应速度。然而,请注意在执行某些操作前确保充分了解其潜在影响;例如,在移除旧内核之前确认新内核稳定运行无问题;而对于关键配置更改则需确保备份好相关设置以便恢复原状态。
207 0
|
4月前
|
消息中间件 缓存 NoSQL
基于Spring Data Redis与RabbitMQ实现字符串缓存和计数功能(数据同步)
总的来说,借助Spring Data Redis和RabbitMQ,我们可以轻松实现字符串缓存和计数的功能。而关键的部分不过是一些"厨房的套路",一旦你掌握了这些套路,那么你就像厨师一样可以准备出一道道饕餮美食了。通过这种方式促进数据处理效率无疑将大大提高我们的生产力。
170 32
|
6月前
|
机器学习/深度学习 人工智能 缓存
MHA2MLA:0.3%数据微调!复旦团队开源推理加速神器,KV缓存狂降96.87%
MHA2MLA是复旦大学、华东师范大学、上海AI Lab等机构联合推出的数据高效微调方法,通过引入多头潜在注意力机制(MLA),显著优化基于Transformer的LLM推理效率,降低推理成本。
201 1
MHA2MLA:0.3%数据微调!复旦团队开源推理加速神器,KV缓存狂降96.87%
|
8月前
|
缓存 NoSQL Java
springboot怎么使用rides缓存方法的返回值 完整例子
通过上述步骤,我们成功地在 Spring Boot 项目中集成了 Redis 缓存,并通过注解的方式实现了方法返回值的缓存。这种方式不仅提高了系统的性能,还简化了缓存管理的复杂度。使用 Spring Boot 的缓存注解和 Redis,可以轻松地实现高效、可靠的缓存机制。
166 23
|
11月前
|
缓存 Java Shell
Android 系统缓存扫描与清理方法分析
Android 系统缓存从原理探索到实现。
362 15
Android 系统缓存扫描与清理方法分析
|
10月前
|
缓存 NoSQL PHP
Redis作为PHP缓存解决方案的优势、实现方式及注意事项。Redis凭借其高性能、丰富的数据结构、数据持久化和分布式支持等特点,在提升应用响应速度和处理能力方面表现突出
本文深入探讨了Redis作为PHP缓存解决方案的优势、实现方式及注意事项。Redis凭借其高性能、丰富的数据结构、数据持久化和分布式支持等特点,在提升应用响应速度和处理能力方面表现突出。文章还介绍了Redis在页面缓存、数据缓存和会话缓存等应用场景中的使用,并强调了缓存数据一致性、过期时间设置、容量控制和安全问题的重要性。
182 5
|
10月前
|
存储 缓存 监控
利用 Redis 缓存特性避免缓存穿透的策略与方法
【10月更文挑战第23天】通过以上对利用 Redis 缓存特性避免缓存穿透的详细阐述,我们对这一策略有了更深入的理解。在实际应用中,我们需要根据具体情况灵活运用这些方法,并结合其他技术手段,共同保障系统的稳定和高效运行。同时,要不断关注 Redis 缓存特性的发展和变化,及时调整策略,以应对不断出现的新挑战。
167 10