Beispiel #1
0
        static public void SetList <T>(T[] list, Func <T, object> idExp, Func <T, long> incr = null)
        {
            if (null == list || !list.Any())
            {
                return;
            }

            var SequenceKey = KeyFactory.GetSequenceKey <T>();

            var serializer = Engine.SerializerFactory.Create();

            var kvs = list.ToDictionary(x => (RedisKey)KeyFactory.GetUrnKey <T>(idExp(x)), y => (RedisValue)serializer.Serialize(y));
            //var ids = list.Select(x => new SortedSetEntry(idExp(x).ToString(), Engine.RedisDB.StringIncrement(SequenceKey)));
            var ids = list.Select(x => new SortedSetEntry(idExp(x).ToString(), incr(x)));
            //var sequence = ids.Select(x => x.Score).ToList();
            //var ids = list.Select(x => (RedisValue)idExp(x).ToString());

            var idKey = KeyFactory.GetIdKey <T>();

            var trans = Engine.RedisDB.CreateTransaction();

            //trans.SetAddAsync(idKey, ids.ToArray());
            trans.SortedSetAddAsync(idKey, ids.ToArray());
            trans.StringSetAsync(kvs.ToArray());

            if (!trans.Execute())
            {
                throw new ApplicationException("写入事务失败!");
            }
        }
Beispiel #2
0
        static public PageList <T> GetList <T>(long pageIndex, int pageSize, bool orderByDesc = false)
        {
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }

            if (pageSize < 1)
            {
                pageSize = 1;
            }

            long skip;

            var idKey = KeyFactory.GetIdKey <T>();

            var total = Engine.RedisDB.SortedSetLength(idKey);

            if (0 == total)
            {
                return(new PageList <T>(new T[] {}, 0, pageSize, 1));
            }

            if (orderByDesc)
            {
                skip = total - (pageIndex * pageSize);
            }
            else
            {
                skip = (pageIndex - 1) * pageSize;
            }

            if (skip + pageSize > total || skip < 0)
            {
                throw new ApplicationException("超出最大数据量范围!");
            }

            var ids = Engine.RedisDB.SortedSetRangeByRank(idKey, skip, skip + pageSize - 1);

            var idKeys = ids.Select(x => (RedisKey)KeyFactory.GetUrnKey <T>(x)).ToArray();

            var jsonObjs = Engine.RedisDB.StringGet(idKeys);

            var serializer = Engine.SerializerFactory.Create();

            var list = jsonObjs.Select(x => serializer.Deserialize <T>(x));

            if (orderByDesc)
            {
                list = list.Reverse();
            }

            return(new PageList <T>(list, total, pageSize, pageIndex));
        }