Redis学习笔记~是时候为Redis实现一个仓储了,RedisRepository来了

回到目录

之前写了不少关于仓储的文章,所以,自己习惯把自己叫仓储大叔,上次写的XMLRepository得到了大家的好评,也有不少朋友给我发email,进行一些知识的探讨,今天主要来实现一个RedisRepository,它始终是集成IRepository接口的,我这里的Redis仓储主要服务为复杂类型的业务,对于只存string这种需求,不需要使用它。

对于Redis仓储和说,它与XML仓储有些不同,由于XML文件一般存储在WWW服务器,所以没有网络通讯问题,而redis一般部署在第三台服务器上,我们一般称为NoSQL服务器,它与WWW通讯是通过socket协议完成的,正是如些,我们在进行仓储设计时,应该考虑到如何去释放它的资源,因为这种资源是非托管的,所以需要人为干预一下,.net提供了using关键字来做这事,而每个动作写using这显然是不友好的,所以,我这个redis仓储是在析构方法里完成对资源的销毁的,请看源代码:

首先是redis基类,它是实现统一操作的前提

   /// <summary>
/// Redis实体基类,所有redis实体类都应该集成它
/// </summary>
public abstract class RedisEntity
{
public RedisEntity()
{
RootID = Guid.NewGuid().ToString();
}
/// <summary>
/// Redis实体主键,方法查询,删除,更新等操作
/// </summary>
public virtual string RootID { get; set; }
}

下面才是RedisRepository仓储的代码

    /// <summary>
/// Redis仓储实现
/// </summary>
public class RedisRepository<TEntity> :
IDisposable,
IRepository<TEntity>
where TEntity : RedisEntity
{
IRedisClient redisDB;
IRedisTypedClient<TEntity> redisTypedClient;
IRedisList<TEntity> table;
public RedisRepository()
{
redisDB = RedisManager.GetClient();
redisTypedClient = redisDB.GetTypedClient<TEntity>();
table = redisTypedClient.Lists[typeof(TEntity).Name];
} #region IRepository<TEntity>成员
public void SetDbContext(IUnitOfWork unitOfWork)
{
throw new NotImplementedException();
} public void Insert(TEntity item)
{
if (item != null)
{
redisTypedClient.AddItemToList(table, item);
  redisDB.Save();
}
} public void Delete(TEntity item)
{
  if (item != null)
            {
                var entity = Find(item.RootID);
                redisTypedClient.RemoveItemFromList(table, entity);
                redisDB.Save();
            }
} public void Update(TEntity item)
{
if (item != null)
{
var old = Find(item.RootID);
if (old != null)
{
redisTypedClient.RemoveItemFromList(table, old);
redisTypedClient.AddItemToList(table, item);
redisDB.Save();
}
}
} public IQueryable<TEntity> GetModel()
{
return table.GetAll().AsQueryable();
} public TEntity Find(params object[] id)
{
return table.Where(i => i.RootID == (string)id[]).FirstOrDefault();
}
#endregion #region IDisposable成员
public void Dispose()
{
this.ExplicitDispose();
}
#endregion #region Protected Methods /// <summary>
/// Provides the facility that disposes the object in an explicit manner,
/// preventing the Finalizer from being called after the object has been
/// disposed explicitly.
/// </summary>
protected void ExplicitDispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected void Dispose(bool disposing)
{
if (disposing)//清除非托管资源
{
table = null;
redisTypedClient = null;
redisDB.Dispose();
}
}
#endregion #region Finalization Constructs
/// <summary>
/// Finalizes the object.
/// </summary>
~RedisRepository()
{
this.Dispose(false);
}
#endregion
}
}

下面是在控制台中进行仓储的调用

        IRepository<Car> repository = new Redis.Data.Core.RedisRepository<Car>();
repository.Insert(new Car { ID = , Name = "占" });
var entity = repository.GetModel().Where(i => i.ID == ).FirstOrDefault();
entity.Name = "修改了";
repository.Update(entity);
repository.GetModel().ToList().ForEach(e =>
{
Console.WriteLine(e.ID + "/" + e.RootID + "/" + e.Name);
});

下面是实现的结果的截图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAa8AAACHCAIAAAAugUG5AAAW70lEQVR4nO1b679dVXU9nyu2gmAVWgvykGcSXgGBPHiJkgABgkoiJASEBELehCCQBMIjIRAgPAVrFQWEVrE8q1jZgFU+9kP/oH44l+POnmuONeZa+yQ3947xW7/722euucYYc655Nxc5Dv7uuNuL1xeOv/0Lx98xXIeccMchJ6w+5ITVh3xz9Rcn1p1fPPHOvx+uk9b8w2idvPZLw3XK2i+dsvbQU9Ydesq6Q09dd+ip6w47df1hp60/7LT1Xx6uGRu+PGPD4TM2HD5zw+EzNx4+c+MRMzceMWvjEbM2HTFr01dmbfrK6cN11z+eMbG+esbmr545sb521uavnXX3cB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f/gBUEQZji0N+GgiAIEwj974wMgyAIwkGJ8b0Ns/8arteoIAiTCN5/mAb/zbq9VfxG06tQEIRJBPyHIXhhNalvCPF/8U2jV2G0L15mwR/VzD/TkheZ9V9w5UlLZL1MnJTmHdrkkC7jn+lDVDrUnyYFLBe6l1DHgB+mrpCQx5D82X7IHuwESdFpAXudIBOkJYeMl87yeD6Z5yiYWnr3gH+RsrplciHOLH+lf4Yze7zgXor71v5IGuOF2geBlieXJOmkRZs/vQCKL5hC3EcvoXjayBwGzLR5WmUemhZ4bzX18v55iRr/JGf2eMG9hFrnncXGWtfLCln+zs+sKOAZBbPqIbdTCl5DkxPQud3OWNitshG0tKTP4snzrNp4tpayGpO6Sc6aer1+2oY3qXsM9YfR9epNHge62bqwbrGfLH9BvXar2fdqOvGkDdsfewpUhxOmJsCtJK/Edr/9kIwnn0PTAPKT85HMb1IAPvFWMpI1z+hm/XRKi9Y7cPrZNsZYwgm8bnFLsbQ9Dkpm6sr6SRbLP+MmNK2rwd46acmgd5BPmMrwBgikdabHDhMzScw0WCHGP46TfsitqGI7swOGgTeJj3f6CWwkn/vSHZhW4HqzfnrhCflJxi2P7VuWpB1v/+wIJZMxCQaTM2WRvEic5g1f9taTyUAa55fFSX5vq0Yu6iGrEjoL8r179J771fV4ksxlfjB5kgdXUbZVzDD6aeNMEJDwDFMW/ETiI5387HRiLXJki/nJosh6a3QZDzX9qdTKpo1Dl6m3zM9YeUAcCGW1Ogyjn8mzNjm7BUCmTSk0LbSDILl9KsnjxbO6HRUmv4AflNbs+8vg1dsY4DivnqTyfPZSL+hbY35L7ZF2hFFn6kpK2GTsh+lbAY9XppfssSX5cfdGVq2HpJ+k1TYJBpMjCIJwAJB8PXmvYO+VjdmycoIgCAce3h+2TDIZacf1NhQEYZKi82/ZZWcr44IgCIIgCIIgCIIgTBP8PxCgzyvWhVMGAAAAAElFTkSuQmCC" alt="" />

回到目录

上一篇:AppCompatActivity工具栏的设置(返回操作)


下一篇:java爬取网页上qq号,邮箱号等