コード例 #1
0
        public void KeyRename()
        {
            IRedis redis = ServiceStackRedis.Default;
            var    key   = Guid.NewGuid().ToString();
            var    value = Guid.NewGuid().ToString();

            redis.StringSet(key, value);
            Assert.IsTrue(redis.KeyExists(key));

            var key2     = Guid.NewGuid().ToString();
            var renamed1 = redis.KeyRename(key, key2);

            Assert.IsTrue(renamed1);

            Assert.IsFalse(redis.KeyExists(key));
            Assert.IsTrue(redis.KeyExists(key2));

            redis.KeyDelete(key2);
            try {
                var renamed12 = redis.KeyRename(key, key2);
                Assert.Fail();
            }
            catch (Exception ex) {
                Assert.IsTrue(ex is RedisException);
            }
        }
コード例 #2
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);
        }
コード例 #3
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);
        }
コード例 #4
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);
            }
        }