コード例 #1
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);
        }