Skip to content

Instantly share code, notes, and snippets.

@rvhuang
Created February 13, 2017 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvhuang/5ca21579ed17002755ab5ac91ee73ced to your computer and use it in GitHub Desktop.
Save rvhuang/5ca21579ed17002755ab5ac91ee73ced to your computer and use it in GitHub Desktop.
A Redis list enumerator based on StackExchange.Redis library.
using StackExchange.Redis;
using System;
using System.Collections;
using System.Collections.Generic;
namespace ModelWorkshop.Scheduling.Redis
{
internal class RedisCollectionEnumerator<TItem> : IEnumerator<TItem>
{
#region Fields
private readonly RedisKey key;
private readonly IDatabase db;
private readonly Func<RedisValue, TItem> converter;
private long index;
#endregion
#region Event
public event EventHandler Disposed;
#endregion
#region Constructor
public RedisCollectionEnumerator(IDatabase db, RedisKey key, Func<RedisValue, TItem> converter)
{
this.db = db;
this.key = key;
this.converter = converter;
this.index = -1;
}
#endregion
#region Properties
public TItem Current
{
get
{
if (this.index < 0)
return default(TItem);
else
return this.converter(this.db.ListGetByIndex(this.key, this.index));
}
}
object IEnumerator.Current
{
get { return this.Current; }
}
#endregion
#region Methods
bool IEnumerator.MoveNext()
{
if (this.index + 1 < this.db.ListLength(this.key))
{
this.index++;
return true;
}
return false;
}
void IEnumerator.Reset()
{
this.index = -1;
}
void IDisposable.Dispose()
{
if (this.Disposed != null) this.Disposed(this, EventArgs.Empty);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment