Redis學習筆記~是時候(hou)為(wei)Redis實現(xian)一個(ge)倉儲了(le),RedisRepository來了(le)
之前寫了不少關于倉儲的文章,所以,自己習慣把自己叫倉儲大叔,上次寫的XMLRepository得到了大家的好評,也(ye)有不少朋友給(gei)我(wo)發email,進行一(yi)(yi)些知識的(de)探(tan)討,今天(tian)主要來實(shi)現一(yi)(yi)個RedisRepository,它始終是集成IRepository接口(kou)的(de),我(wo)這里(li)的(de)Redis倉儲主要服務為復雜類型(xing)的(de)業務,對于(yu)只存string這種需(xu)求,不需(xu)要使(shi)用(yong)它。
對于(yu)Redis倉儲(chu)(chu)(chu)和說,它(ta)與(yu)XML倉儲(chu)(chu)(chu)有些不同,由于(yu)XML文(wen)件一(yi)般存儲(chu)(chu)(chu)在(zai)WWW服務(wu)器,所以沒有網絡通(tong)訊(xun)(xun)問(wen)題,而redis一(yi)般部署在(zai)第三臺服務(wu)器上,我們一(yi)般稱為(wei)NoSQL服務(wu)器,它(ta)與(yu)WWW通(tong)訊(xun)(xun)是通(tong)過socket協議完成(cheng)的(de),正是如些,我們在(zai)進行倉儲(chu)(chu)(chu)設計時(shi),應(ying)該(gai)考慮到如何去釋放它(ta)的(de)資(zi)源(yuan),因為(wei)這種(zhong)資(zi)源(yuan)是非托管的(de),所以需要人為(wei)干(gan)預(yu)一(yi)下,.net提供了using關(guan)鍵字來(lai)做這事,而每個動作寫(xie)using這顯然是不友好的(de),所以,我這個redis倉儲(chu)(chu)(chu)是在(zai)析構方法里完成(cheng)對資(zi)源(yuan)的(de)銷(xiao)毀的(de),請看源(yuan)代碼:
首先是(shi)redis基類(lei),它(ta)是(shi)實現統一操(cao)作(zuo)的前(qian)提
/// <summary> /// Redis實體基(ji)類,所(suo)有redis實體類都應(ying)該集成它 /// </summary> public abstract class RedisEntity { public RedisEntity() { RootID = Guid.NewGuid().ToString(); } /// <summary> /// Redis實體主(zhu)鍵,方法查詢,刪除,更新(xin)等操作 /// </summary> public virtual string RootID { get; set; } }
下(xia)面才是RedisRepository倉儲(chu)的代碼
/// <summary> /// Redis倉儲(chu)實現 /// </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[0]).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)//清(qing)除非托管資源(yuan) { table = null; redisTypedClient = null; redisDB.Dispose(); } } #endregion #region Finalization Constructs /// <summary> /// Finalizes the object. /// </summary> ~RedisRepository() { this.Dispose(false); } #endregion } }
下面(mian)是(shi)在(zai)控(kong)制臺(tai)中進行倉(cang)儲的調用
IRepository<Car> repository = new Redis.Data.Core.RedisRepository<Car>(); repository.Insert(new Car { ID = 3, Name = "占(zhan)" }); var entity = repository.GetModel().Where(i => i.ID == 3).FirstOrDefault(); entity.Name = "修改了"; repository.Update(entity); repository.GetModel().ToList().ForEach(e => { Console.WriteLine(e.ID + "/" + e.RootID + "/" + e.Name); });
下面(mian)是(shi)實現的(de)結果(guo)的(de)截圖(tu)