Last active
October 16, 2017 17:37
-
-
Save msarchet/4900588 to your computer and use it in GitHub Desktop.
Sometime you need to make sure that Redis is actually updating what you want to update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Sometimes you need to just update something and assure that it happens | |
*/ | |
public static class GenericReplaceLock | |
{ | |
public static void UpdateListItem<T>(this IRedisClientsManager Manager, string key, T old, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromList(r.Lists[key], old)); | |
transaction.QueueCommand(r => r.AddItemToList(r.Lists[key], newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateListItem<T>(this IRedisClientsManager Manager, IRedisList<T> list, T old, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromList(list, old)); | |
transaction.QueueCommand(r => r.AddItemToList(list, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateHashItem<T, TKey>(this IRedisClientsManager Manager, string hashkey, TKey itemkey, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveEntryFromHash(r.GetHash<TKey>(hashkey), itemkey)); | |
transaction.QueueCommand(r => r.SetEntryInHash(r.GetHash<TKey>(hashkey), itemkey, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateHashItem<T, TKey>(this IRedisClientsManager Manager, IRedisHash<TKey, T> hash, TKey itemkey, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveEntryFromHash(hash, itemkey)); | |
transaction.QueueCommand(r => r.SetEntryInHash(hash, itemkey, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSetItem<T>(this IRedisClientsManager Manager, string setkey, T old, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSet(r.Sets[setkey], old)); | |
transaction.QueueCommand(r => r.AddItemToSet(r.Sets[setkey], newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSetItem<T>(this IRedisClientsManager Manager, IRedisSet<T> set, T old, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSet(set, old)); | |
transaction.QueueCommand(r => r.AddItemToSet(set, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisClientsManager Manager, string setkey, T old, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(r.SortedSets[setkey], old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(r.SortedSets[setkey], newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisClientsManager Manager, IRedisSortedSet<T> set, T old, T newer) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(set, old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(set, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisClientsManager Manager, string setkey, T old, T newer, double score) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(r.SortedSets[setkey], old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(r.SortedSets[setkey], newer, score)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisClientsManager Manager, IRedisSortedSet<T> set, T old, T newer, double score) | |
{ | |
using (var Client = Manager.GetClient()) | |
using (var redis = Client.As<T>()) | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(set, old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(set, newer, score)); | |
transaction.Commit(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Sometimes you need to just update something and assure that it happens | |
*/ | |
public static class TypedClientUpdatExtensions | |
{ | |
public static void UpdateListItem<T>(this IRedisTypedClient<T> redis, string key, T old, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromList(r.Lists[key], old)); | |
transaction.QueueCommand(r => r.AddItemToList(r.Lists[key], newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateListItem<T>(this IRedisTypedClient<T> redis, IRedisList<T> list, T old, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromList(list, old)); | |
transaction.QueueCommand(r => r.AddItemToList(list, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateHashItem<T, TKey>(this IRedisTypedClient<T> redis, string hashkey, TKey itemkey, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveEntryFromHash(r.GetHash<TKey>(hashkey), itemkey)); | |
transaction.QueueCommand(r => r.SetEntryInHash(r.GetHash<TKey>(hashkey), itemkey, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateHashItem<T, TKey>(this IRedisTypedClient<T> redis, IRedisHash<TKey, T> hash, TKey itemkey, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveEntryFromHash(hash, itemkey)); | |
transaction.QueueCommand(r => r.SetEntryInHash(hash, itemkey, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSetItem<T>(this IRedisTypedClient<T> redis, string setkey, T old, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSet(r.Sets[setkey], old)); | |
transaction.QueueCommand(r => r.AddItemToSet(r.Sets[setkey], newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSetItem<T>(this IRedisTypedClient<T> redis, IRedisSet<T> set, T old, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSet(set, old)); | |
transaction.QueueCommand(r => r.AddItemToSet(set, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisTypedClient<T> redis, string setkey, T old, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(r.SortedSets[setkey], old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(r.SortedSets[setkey], newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisTypedClient<T> redis, IRedisSortedSet<T> set, T old, T newer) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(set, old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(set, newer)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisTypedClient<T> redis, string setkey, T old, T newer, double score) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(r.SortedSets[setkey], old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(r.SortedSets[setkey], newer, score)); | |
transaction.Commit(); | |
} | |
} | |
public static void UpdateSortedSetItem<T>(this IRedisTypedClient<T> redis, IRedisSortedSet<T> set, T old, T newer, double score) | |
{ | |
using (var transaction = redis.CreateTransaction()) | |
{ | |
transaction.QueueCommand(r => r.RemoveItemFromSortedSet(set, old)); | |
transaction.QueueCommand(r => r.AddItemToSortedSet(set, newer, score)); | |
transaction.Commit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment