title: ServiceStack.Redis store objects with timeout and retrieve by key
body: <p>I'm trying to move from memcached to redis using the ServiceStack.Redis client. I would like to be able to simply check to see if the Redis cache has the items by key and if not add them with an expiration timeout. Then later retrieve them if they exist. </p>
  
  <p>To test this I have created a simple ASP.NET WebApi project and modified the ValuesController with these two methods.</p>
  
  <pre><code>public class ValuesController : ApiController
  {
      public IEnumerable&lt;string&gt; Get()
      {
          using (var redisClient = new RedisClient("localhost"))
          {
              IRedisTypedClient&lt;IEnumerable&lt;SampleEvent&gt;&gt; redis = redisClient.As&lt;IEnumerable&lt;SampleEvent&gt;&gt;();
  
              if (!redis.ContainsKey("urn:medications:25"))
              {
                  var medsWithID25 = new List&lt;SampleEvent&gt;();
                  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&lt;IEnumerable&lt;SampleEvent&gt;&gt; redis = redisClient.As&lt;IEnumerable&lt;SampleEvent&gt;&gt;();
              IEnumerable&lt;SampleEvent&gt; events = redis.GetById("urn:medications:25");
  
              if (events != null)
              {
                  return events.Where(m =&gt; m.ID == id).SingleOrDefault();
              }
              else
                  return null;
          }
      }
  }
  </code></pre>
  
  <p>This doesn't seem to work. The redis.GetById always returns null. What am I doing wrong?</p>
  
  <p>Thanks.</p>
  
  <p>UPDATE 1:</p>
  
  <p>If I change the line where I get the data to: </p>
  
  <pre><code>IEnumerable&lt;SampleEvent&gt; events = redis.GetValue("urn:medications:25");
  </code></pre>
  
  <p>Then I get my objects back but even after the timeout should have removed it.</p>