zoukankan      html  css  js  c++  java
  • Redis学习笔记~把redis放在DATA层,作为一种数据源,我认为更合理,也更符合我的面向对象原则

    回到目录

    redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
    Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。[1]
    Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。
    redis的官网地址,非常好记,是redis.io。(特意查了一下,域名后缀io属于国家域名,是british Indian Ocean territory,即英属印度洋领地)

    一 一个Repository数据库操作基类,提供了通用的操作属性和方法

    
    
    1 namespace Data
    2 {
    3     public abstract class RepositoryBase
    4     {
    5         protected static RedisClient_SortedSet redisClient = new RedisClient_SortedSet();
    6         public static DataClasses1DataContext db = new DataClasses1DataContext();
    7     }
    8 }
    
    

    二 接口定义操作规范

    
    
     1 namespace Data
     2 {
     3     /// <summary>
     4     /// 接口规范
     5     /// </summary>
     6     /// <typeparam name="TEntity"></typeparam>
     7     public interface IRepository<TEntity> where TEntity : class
     8     {
     9         IQueryable<TEntity> GetModel();
    10     }
    11 }
    
    

    三 提供不同的实现接口的方式

    
    
     1 namespace Data.Sql //使用SQL实现查询
     2 {
     3     public class PersonRepository : RepositoryBase, IRepository<Res_Item>
     4     {
     5         #region IRepository<Res_Item> 成员
     6 
     7         public IQueryable<Res_Item> GetModel()
     8         {
     9             return db.Res_Item.Select(i => new Res_Item_Ext
    10             {
    11                 ID = i.ID,
    12                 Name = i.Name,
    13                 PackageTime = i.PackageTime,
    14             });
    15         }
    16 
    17         #endregion
    18     }
    19 }
    20 
    21 namespace Data.Redis //使用Redis实现查询
    22 {
    23     public class PersonRepository : RepositoryBase, IRepository<Res_Item>
    24     {
    25 
    26         #region IRepository<Res_Item> 成¨|员?à
    27 
    28         public IQueryable<Res_Item> GetModel()
    29         {
    30             return redisClient.GetList("Res_Item").Cast<Res_Item>().ToList().AsQueryable();
    31         }
    32 
    33         #endregion
    34     }
    35 }
    
    

    四 实体类,由于要被序列化,所以redis需要你的实体类标有Serializable特性

    1    [Serializable]
    2     public partial class Res_Item { }
    3     [Serializable]
    4     public class Res_Item_Ext : Res_Item
    5     {
    6 
    7     }

    五 如何在BLL层去调用你的redis数据库

     1  #region 从数据库中建立一个redis缓存对象
     2 
     3             try
     4             {
     5                 rds.SelectDB(2);
     6                 new Data.Sql.PersonRepository().GetModel().Take(10).ToList().ForEach(i =>
     7                 {
     8                     rds.Add("Res_Item", i.ID, i);
     9  
    10                 });
    11                 rds.BackgroundSave();//异步序列化到磁盘
    12             }
    13             catch (Exception)
    14             {
    15 
    16                 throw;
    17             }
    18             #endregion
    19 
    20             #region
    21             new Data.Redis.PersonRepository().GetModel().ToList().ForEach(i => Console.WriteLine(i.ID + i.Name + i.PackageTime));
    22   #endregion

    回到目录

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    基于分布式锁解决定时任务重复问题
    基于Redis的Setnx实现分布式锁
    基于数据库悲观锁的分布式锁
    使用锁解决电商中的超卖
  • 原文地址:https://www.cnblogs.com/lori/p/2508101.html
Copyright © 2011-2022 走看看