Esempio n. 1
0
        public void ListTest()
        {
            var         cacheKey   = Guid.NewGuid().ToString();
            IRedis      redis      = ServiceStackRedis.Default;
            var         linkList   = new LinkedList <String>();
            const Int32 listLength = 4;

            Action init = () => {
                redis.KeyDelete(cacheKey);
                linkList.Clear();

                for (int i = 0; i < listLength; i++)
                {
                    var cacheValue = Guid.NewGuid().ToString();

                    if ((Guid.NewGuid().GetHashCode() & 1) == 0)
                    {
                        linkList.AddFirst(cacheValue);
                        //ListLeftPush
                        redis.ListLeftPush(cacheKey, linkList.First.Value);
                    }
                    else
                    {
                        linkList.AddLast(cacheValue);
                        //ListLeftPush
                        redis.ListRightPush(cacheKey, linkList.Last.Value);
                    }
                }
            };

            init();
            Assert.AreEqual(linkList.Count, redis.ListLength(cacheKey));


            for (int i = 0; i < listLength; i++)
            {
                RedisField cacheItem;
                if ((Guid.NewGuid().GetHashCode() & 1) == 0)
                {
                    cacheItem = redis.ListLeftPop(cacheKey);
                    Assert.AreEqual(linkList.First.Value, (String)cacheItem);
                    linkList.RemoveFirst();
                }
                else
                {
                    cacheItem = redis.ListRightPop(cacheKey);
                    Assert.AreEqual(linkList.Last.Value, (String)cacheItem);
                    linkList.RemoveLast();
                }

                Assert.AreEqual(linkList.Count, redis.ListLength(cacheKey));
            }

            var cacheEists = redis.KeyExists(cacheKey);

            Assert.IsFalse(cacheEists);
        }
Esempio n. 2
0
        public void ListMultiTest()
        {
            var    cacheKey   = Guid.NewGuid().ToString();
            IRedis redis      = ServiceStackRedis.Default;
            var    linkList   = new List <String>();
            var    listLength = Math.Abs(Guid.NewGuid().GetHashCode() % 5) + 5;

            redis.KeyDelete(cacheKey);
            linkList = Enumerable.Repeat(0, listLength)
                       .Select(x => Guid.NewGuid().ToString())
                       .ToList();

            {
                redis.ListRightPush(cacheKey, linkList.Select(x => (RedisField)x).ToArray());
                Assert.AreEqual(linkList.Count, redis.ListLength(cacheKey));

                for (int i = 0; i < listLength; i++)
                {
                    var cacheItem = redis.ListLeftPop(cacheKey);
                    Assert.AreEqual(linkList[i], (String)cacheItem);
                }

                var cacheEists = redis.KeyExists(cacheKey);
                Assert.IsFalse(cacheEists);
            }

            {
                redis.ListLeftPush(cacheKey, linkList.Select(x => (RedisField)x).ToArray());
                Assert.AreEqual(linkList.Count, redis.ListLength(cacheKey));

                for (int i = 0; i < listLength; i++)
                {
                    var cacheItem = redis.ListRightPop(cacheKey);
                    Assert.AreEqual(linkList[i], (String)cacheItem);
                }

                var cacheEists = redis.KeyExists(cacheKey);
                Assert.IsFalse(cacheEists);
            }
        }