Beispiel #1
0
        public void StringTest() {
            var cacheKey = Guid.NewGuid().ToString();
            IRedis redis = new ServiceStackRedis();

            //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);
        }
Beispiel #2
0
        public void ListTest() {
            var cacheKey = Guid.NewGuid().ToString();
            IRedis redis = new ServiceStackRedis();
            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);
        }
Beispiel #3
0
        public void SortSertTest() {
            var cacheKey = Guid.NewGuid().ToString();
            IRedis redis = new ServiceStackRedis();

            var random = new Random();
            var list = Enumerable.Repeat(0, 20).Select(r => random.Next(100)).Distinct().ToList();

            for (int i = 0; i < list.Count; i++) {
                redis.SortedSetAdd(cacheKey, list[i].ToString(), i);
                var len = redis.SortedSetLength(cacheKey);
                Assert.AreEqual(len, i + 1);
            }

            var values = redis.SortedSetRangeByRank(cacheKey, 0, -1);
            Assert.AreEqual(values.Length, list.Count);
            for (int i = 0; i < list.Count; i++) {
                Assert.AreEqual((String)values[i], list[i].ToString());
            }

            for (int i = 0; i < 3; i++) {
                var index1 = random.Next(list.Count);
                var index2 = redis.SortedSetRank(cacheKey, list[index1].ToString());
                Assert.AreEqual(index1, index2);
            }

            for (int i = 0; i < 3; i++) {
                var index = random.Next(list.Count);
                var value = list[index];
                list.RemoveAt(index);
                var removed = redis.SortedSetRemove(cacheKey, value.ToString());
                Assert.IsTrue(removed);
                var len = redis.SortedSetLength(cacheKey);
                Assert.AreEqual(len, list.Count);
            }

            Assert.IsTrue(redis.SortedSetLength(cacheKey) > 3);
            var removedByRank = redis.SortedSetRemoveRangeByRank(cacheKey, 0, 2);
            Assert.AreEqual(removedByRank, 3);
        }
Beispiel #4
0
        public void RedisParallelTest() {

            StackExchange.Redis.IDatabase d;

            var key = "RedisParallelTest";
            var redis = new ServiceStackRedis();
            redis.KeyDelete(key);

            Action action = () => Console.WriteLine(redis.StringIncrement(key));
            Parallel.Invoke(Enumerable.Repeat(1, 100).Select(i => action).ToArray());
        }
Beispiel #5
0
        public void HashTest() {
            var cacheKey = Guid.NewGuid().ToString();
            IRedis redis = new ServiceStackRedis();

            var hashListLength = Math.Abs(Guid.NewGuid().GetHashCode() % 24) + 8;
            var names = new String[hashListLength].ToList();
            var values = new String[hashListLength];

            var list = new List<RedisEntry>();
            for (int i = 0; i < 8; i++) {
                names[i] = Guid.NewGuid().ToString();
                values[i] = Guid.NewGuid().ToString();
                list.Add(new RedisEntry(names[i], values[i]));
            }
            redis.HashSet(cacheKey, list);
            Assert.AreEqual(redis.HashLength(cacheKey), list.Count);

            for (int i = 8; i < hashListLength; i++) {
                names[i] = Guid.NewGuid().ToString();
                values[i] = Guid.NewGuid().ToString();

                if ((Guid.NewGuid().GetHashCode() & 1) == 0) {
                    redis.HashSet(cacheKey, new RedisEntry(names[i], values[i]));
                }
                else {
                    redis.HashSet(cacheKey, names[i], values[i]);
                }
            }

            Assert.AreEqual(redis.HashLength(cacheKey), hashListLength);

            var hash = redis.HashGetAll(cacheKey);
            Assert.AreEqual(hash.Length, hashListLength);

            for (int i = 0; i < hashListLength; i++) {
                Assert.IsTrue(hash[i].Name == names[i]);
                Assert.IsTrue(hash[i].Value == values[i]);
            }

            for (int i = 0; i < 8; i++) {
                var index = Math.Abs(Guid.NewGuid().GetHashCode() % names.Count);
                var cacheItem = redis.HashGet(cacheKey, names[index]);
                Assert.IsTrue((String)cacheItem == values[index]);
            }

            for (int i = 0; i < 8; i++) {
                if ((Guid.NewGuid().GetHashCode() & 1) == 0) {
                    var index = Math.Abs(Guid.NewGuid().GetHashCode() % names.Count);
                    var deleted = redis.HashDelete(cacheKey, names[index]);
                    if (!deleted) {
                        Debugger.Launch();
                    }
                    Assert.IsTrue(deleted);
                    names.RemoveAt(index);
                }
                else {
                    var deleted = redis.HashDelete(cacheKey, Guid.NewGuid().ToString());
                    Assert.IsFalse(deleted);
                }
            }
        }