Search code examples
c#redisservicestack.redis

What does IRedisClient.As<T>() do behind the scenes?


I'm curently using the c# ServiceStack RedisClient in the following way

   using (var cache = new BasicRedisClientManager(readWriteHosts).ClientFactory.GetClient())
   {
        var r = cache.As<Foo>();
        var myItem = r.GetById(123);
   }

I want to know what happens behind the scenes with this? How does Redis know which type relates to which key? It can't be inspecting each type for a match, that would be too slow. When I set the object, I'm serialising it myself and adding it as string - so it can't know from there either.

It works fantastically, I even tried changing properties and namespaces of the type to see what happens and it just handles it. Does anyone know How?


Solution

  • When in doubt you can just read the source code, i.e. it's effectively just returning a Typed Generic RedisClient:

    public IRedisTypedClient<T> As<T>()
    {
        return new RedisTypedClient<T>(this);
    }
    

    Whilst the source code for RedisTypedClient shows exactly what it does, this existing answer explains roughly how it works.