Last active
December 24, 2015 22:19
-
-
Save xmlviking/6871977 to your computer and use it in GitHub Desktop.
Redis BookStack Expire extension for the Store method. Dan Parnham's BookStack provides an implementation for typed structures to be persisted, queried and removed from the Redis store. Unfortunately for me the library did not incorporate and overloaded feature in which the user could provide the Expire parameter. This version does work with his…
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
public static class BookStackExtensions | |
{ | |
public static Task<T> Store<T>(this RedisConnection redis, int db, T entity, int expire) | |
{ | |
var id = entity.GetType().GetProperty("Id").GetGetMethod().Invoke(entity, new object[0]); | |
return redis.Store(db, id, entity, expire); | |
} | |
public static Task<T> Store<T>(this RedisConnection redis, int db, object id, T entity, int expire) | |
{ | |
var urn = string.Format("urn:{0}:{1}", typeof(T).Name.ToLower(), id); | |
var ids = string.Format("ids:{0}", typeof (T).Name.ToLower()); | |
var identity = string.Format("id:{0}", typeof (T).Name.ToLower()); | |
var tasks = new[] { | |
redis.Strings.Set(db, urn, entity.ToJson()), | |
redis.Keys.Expire(db, urn, expire), | |
redis.Sets.Add(db, ids, id.ToString()), | |
redis.Keys.Expire(db,ids,expire), | |
redis.Keys.Expire(db,identity,expire) | |
}; | |
return Task.Factory.ContinueWhenAll(tasks, t => entity); | |
} | |
} |
Updated gist method Store with the explict expiration of id, urn and ids.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initial gist submission.
Redis .NET api BookStack extension which will facilitate the Expire keyword for Redis.
One additional option would be to provide direct accessibility for the Booksleeve Keys structure.
Eric F. Cotter