public void HashTest() { var cacheKey = Guid.NewGuid().ToString(); IRedis redis = ServiceStackRedis.Default; var count = 10; var names = new String[count].ToList(); var values = new String[count]; for (int i = 0; i < count; i++) { names[i] = Guid.NewGuid().ToString(); values[i] = Guid.NewGuid().ToString(); } var list = Enumerable.Range(0, count) .Select(i => new RedisEntry(names[i], values[i])) .ToArray(); redis.HashSet(cacheKey, list); Assert.AreEqual(redis.HashLength(cacheKey), count); var array = redis.HashGet(cacheKey, names.Select(x => (RedisField)x).ToArray()); for (int i = 0; i < count; i++) { Assert.IsTrue(array[i] == values[i]); } var hash = redis.HashGetAll(cacheKey); Assert.AreEqual(hash.Length, count); for (int i = 0; i < count; i++) { Assert.IsTrue(hash[i].Name == names[i]); Assert.IsTrue(hash[i].Value == values[i]); } for (int i = 0; i < count; i++) { var cacheItem = redis.HashGet(cacheKey, names[i]); Assert.IsTrue((String)cacheItem == values[i]); } for (int i = 0; i < count; i++) { var deleted = redis.HashDelete(cacheKey, names[i]); Assert.IsTrue(deleted); } var exist = redis.KeyExists(cacheKey); Assert.IsFalse(exist); }
/// <summary> /// 增加数据 /// </summary> /// <param name="t">HuobiProject.Models</param> /// <returns> /// True:成功 /// False:失败 /// </returns> bool ICURD <TTicket> .Create(TTicket t) { using (TransactionScope scope = new TransactionScope()) { Random rand = new Random((int)Tools.Ticks()); string id = Tools.Ticks() + "" + rand.Next(1000, 10000); t.TicketId = id; t.StatusId = 700001; t.CreateTime = DateTime.Now; t.ActiveTime = t.CreateTime; m_db.TTicket.Add(t); m_db.SaveChanges(); m_redis.HashSet(RedisDB, t.TicketId, t); scope.Complete(); } return(true); }
/// <summary> /// 根据关键字查询文章 /// </summary> /// <param name="keyword">关键字</param> /// <param name="langId">语言id</param> /// <returns></returns> dynamic IArticle.SearchByKeyword(string keyword, int langId) { string key_id_md5 = EncryptProvider.Md5((keyword + langId), MD5Length.L16); //检查Redis是否有记录 if (m_redis.HashExists("SearchByKeyword", key_id_md5)) { return(m_redis.HashGet <dynamic>("SearchByKeyword", key_id_md5)); } /* * 1.将相关数据查询 * 2.匹配内容 * 3.匹配度排序 */ IQueryable <TType> types = m_iType.RetrieveArticleTypesByLangId(langId); string partten = @"(?<=[??!!::。.\n]*)[^??!!::。.\n]*" + keyword + "[^??!!::。.\n]*.(?<=[??!!::。.\n]*)"; //string partten = @"(?<=[。|?|!|:|.|?|!|:])*" + keyword + ".*?(?<=[。|?|!|:|.|?|!|:])"; dynamic key_search = types.SelectMany(sm => m_db.TArticleLang .Include(i => i.User) .Include(i => i.Type) .Include(i => i.THelpful) .Where( L => sm.TypeId == L.TypeId && ( L.Title.Contains(keyword) || L.Content.Contains(keyword) ) ) .Select(s => new { s.ArticleLangId, s.Title, s.Commentable, s.Flag, s.ArticleId, s.UpdateTime, s.CreateTime, Type = new { s.Type.TypeId, s.Type.Name, s.Type.Description, s.Type.Parent }, User = new { s.User.UserId, s.User.UserName, s.User.Photo }, Content = Regex.Match(s.Content, partten).Value, CommentCount = m_db.TComment.Where(w => w.ArticleLangId == s.ArticleLangId).Count(), Helpful = s.THelpful.Where(w => w.ArticleLangId == s.ArticleLangId && w.Helpful == 1).Count(), })) .OrderByDescending(ob => Regex.Matches(ob.Title, keyword).Count + Regex.Matches(ob.Content, keyword).Count) .ToList(); // Regex.Matches(s.Content, keyword) // .OrderByDescending(ob => Regex.Matches(ob.Value, keyword).Count) // .FirstOrDefault().Value //记录搜索结果到Redis m_redis.HashSet("SearchByKeyword", key_id_md5, key_search, TimeSpan.FromMinutes(30)); return(JArray.FromObject(key_search)); }