Skip to content

Instantly share code, notes, and snippets.

@glikoz
Created October 23, 2013 14:22
Show Gist options
  • Save glikoz/7119769 to your computer and use it in GitHub Desktop.
Save glikoz/7119769 to your computer and use it in GitHub Desktop.
[DependencyRegisterar(Lifetime = LifetimeManagers.PerRequest)]
public class RedisRepository : IRedisClientsManager, IBasicPersistenceProvider
{
#region Members
private const string CONFIGURATION_PREFIX = "cfg:";
private const string PERFORMANCE_PREFIX = "prf:";
private const string HASHSET_PREFIX = "set:";
private IRedisClientsManager _Manager;
#endregion
#region Properties
/// <summary>
/// Gets the current Redis Repository
/// </summary>
public static RedisRepository Current
{
get
{
return Core.Resolve<RedisRepository>();
}
}
/// <summary>
/// Gets the Redis clients manager
/// </summary>
public IRedisClientsManager Manager
{
get
{
return _Manager;
}
}
#endregion
#region Initialization
/// <summary>
/// Initialize a new instance of this class
/// </summary>
/// <param name="manager"></param>
public RedisRepository(IRedisClientsManager manager)
{
_Manager = manager;
}
#endregion
#region Public Methods
public IDisposable GetLock(LockEnum lockEnum, TimeSpan timeout)
{
return _Manager.GetClient().AcquireLock(lockEnum.ToString(), timeout);
}
public long GetAid(string code)
{
using (var context = CreateOperationContext())
{
return context.Client.IncrementValue("aid:" + code);
}
}
public IEnumerable<T> GetValuesFromHashes<T>(IEnumerable<string> hashIds, string key)
{
//TODO: Performance optimization
List<T> result = new List<T>();
using (var context = CreateOperationContext())
{
foreach (var hash in hashIds)
{
result.Add(GetHashItem<T>(hash, key));
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment