예제 #1
0
        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);
        }
예제 #2
0
        /// <summary>
        /// 通过问题id获取问题下详情列表
        /// </summary>
        /// <param name="typeId">类型id</param>
        /// <returns></returns>
        List <TType> ITicket.RetrieveByQuestionId(int typeId)
        {
            if (m_redis.HashExists("TicketQuestion", typeId))
            {
                return(m_redis.HashGet <List <TType> >("TicketQuestion", typeId));
            }

            List <TType> list = m_db.TType
                                .Where(w => w.ParentId == typeId)
                                .ToList();

            m_redis.HashSet("TicketQuestion", typeId, list, TimeSpan.FromMinutes(30));

            return(list);
        }
예제 #3
0
        /// <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));
        }