Skip to content

Instantly share code, notes, and snippets.

@xmlviking
Last active December 24, 2015 22:19
Show Gist options
  • Save xmlviking/6871977 to your computer and use it in GitHub Desktop.
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…
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);
}
}
@xmlviking
Copy link
Author

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