ServiceStack.Redis クライアントを使用して memcached から redis に移行しようとしています。Redis キャッシュにキーでアイテムがあるかどうかを確認し、そうでない場合は有効期限のタイムアウトでそれらを追加できるようにしたいと思います。それらが存在する場合は、後でそれらを取得します。
これをテストするために、単純な ASP.NET WebApi プロジェクトを作成し、これら 2 つのメソッドで ValuesController を変更しました。
public class ValuesController : ApiController
{
public IEnumerable<string> Get()
{
using (var redisClient = new RedisClient("localhost"))
{
IRedisTypedClient<IEnumerable<SampleEvent>> redis = redisClient.As<IEnumerable<SampleEvent>>();
if (!redis.ContainsKey("urn:medications:25"))
{
var medsWithID25 = new List<SampleEvent>();
medsWithID25.Add(new SampleEvent() { ID = 1, EntityID = "25", Name = "Digoxin" });
medsWithID25.Add(new SampleEvent() { ID = 2, EntityID = "25", Name = "Aspirin" });
redis.SetEntry("urn:medications:25", medsWithID25);
redis.ExpireIn("urn:medications:25", TimeSpan.FromSeconds(30));
}
}
return new string[] { "1", "2" };
}
public SampleEvent Get(int id)
{
using (var redisClient = new RedisClient("localhost"))
{
IRedisTypedClient<IEnumerable<SampleEvent>> redis = redisClient.As<IEnumerable<SampleEvent>>();
IEnumerable<SampleEvent> events = redis.GetById("urn:medications:25");
if (events != null)
{
return events.Where(m => m.ID == id).SingleOrDefault();
}
else
return null;
}
}
}
これはうまくいかないようです。redis.GetById は常に null を返します。私は何を間違っていますか?
ありがとう。
更新 1:
データを取得する行を次のように変更すると:
IEnumerable<SampleEvent> events = redis.GetValue("urn:medications:25");
その後、オブジェクトを元に戻しますが、タイムアウト後でもオブジェクトを削除する必要があります。