コード例 #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 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);
            }
        }
コード例 #4
0
        public void StringTest()
        {
            var    cacheKey = Guid.NewGuid().ToString();
            IRedis redis    = ServiceStackRedis.Default;

            //StringGet
            var cacheField = redis.StringGet(cacheKey);

            Assert.IsFalse(cacheField.HasValue);
            Assert.AreEqual((String)cacheField, null);

            //StringSet
            var cacheValue = Guid.NewGuid().ToString();

            redis.StringSet(cacheKey, cacheValue);

            //StringGet again
            cacheField = redis.StringGet(cacheKey);
            Assert.IsTrue(cacheField.HasValue);
            Assert.AreEqual((String)cacheField, cacheValue);

            //KeyDelete
            redis.KeyDelete(cacheKey);
        }
コード例 #5
0
 public override void Expire(String key)
 {
     _redis.KeyDelete(BuildCacheKey(key));
 }