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); } }
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); }
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); } }
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); }
public override void Expire(String key) { _redis.KeyDelete(BuildCacheKey(key)); }