Skip to content

Instantly share code, notes, and snippets.

@msarchet
Created December 4, 2012 17:56
Show Gist options
  • Save msarchet/4206865 to your computer and use it in GitHub Desktop.
Save msarchet/4206865 to your computer and use it in GitHub Desktop.
A Quick Overview of a problem
public static class IRedisClientsManagerExtensions()
{
public static ManagedList<T> GetManagedList(this IRedisClientsManager manager, string key)
{
return new ManagedList<T>(manager, key);
}
}
public class ManagedList<T> : IList<T>
{
private string _key = null;
private IRedisClientsManager _manager;
private ManagedList() { }
public ManagedList(IRedisClientsManager manager, string key)
{
_key = key;
_manager = manager;
}
#region IList<T> Members
private IRedisClient GetClient()
{
return _manager.GetClient();
}
private List<T> getRedisList()
{
using(var redis = GetClient())
{
var client = redis.As<T>();
return client.Lists[_key].ToList();
}
}
public int IndexOf(T item)
{
return getRedisList().IndexOf(item);
}
public void Insert(int index, T item)
{
using(var redis = GetClient())
{
redis.As<T>().Lists[_key].Insert(index, item);
}
}
public void RemoveAt(int index)
{
using(var redis = GetClient())
{
redis.As<T>().Lists[_key].RemoveAt(index);
}
}
public T this[int index]
{
get
{
return getRedisList()[index];
}
set
{
using(var redis = GetClient())
{
redis.As<T>().Lists[_key][index] = value;
}
}
}
#endregion
#region ICollection<T> Members
public void Add(T item)
{
using(var redis = GetClient())
{
redis.As<T>().Lists[_key].Add(item);
}
}
public void Clear()
{
using(var redis = GetClient())
{
redis.As<T>().Lists[_key].Clear();
}
}
public bool Contains(T item)
{
return getRedisList().Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
getRedisList().CopyTo(array, arrayIndex);
}
public int Count
{
get { return getRedisList().Count(); }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
var index = this.IndexOf(item);
if (index != -1)
{
this.RemoveAt(index);
return true;
}
return false;
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
return getRedisList().GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((IEnumerable)getRedisList()).GetEnumerator();
}
#endregion
}
@msarchet
Copy link
Author

The Remove Can be updated if you are using the latest version of ServiceStack.Redis.

@mythz
Copy link

mythz commented Dec 12, 2012

okie i'm going to be really picky about the code-style to get it more consistent with everything else in the code-base.

  • All methods should be PascalCase (i.e. rename getRedisList to GetRedisList())
  • Don't use _underscorePrefixes (camelCase means non-public)
  • Don't use Regions
  • Use 4 spaces for indentation (Ctrl+K+D does autoformat)

But the code itself looks fine :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment