Connection Exception:
First ensure that your Redis server is configured to run on a network reachable IP. Ensure you have changed redis.conf
bind
line from:
bind 127.0.0.1
to
bind 192.168.0.1
Where 192.168.0.1 is the hostname/IP of your Redis server.
Or you will only be able to access the Redis server locally. You should avoid using 0.0.0.0 in place of your local IP, to prevent Redis binding to any public interfaces, thus exposing it to the Internet. (Unless protected by your firewall)
If you have the Redis client installed on your application server, you can check that the Redis server is accessible by running this command from the terminal:
redis-cli -h 192.168.0.1 ping
This answer here steps through the process of diagnosing the connection issues.
If you are still getting exceptions while connecting ensure that your firewall on your application machine allows the outgoing connection to be established, and on your Redis service your firewall allows incoming connections on that port from your application server. Ubuntu uses IPTables for firewalling.
Using ServiceStack.Redis without the ServiceStack Platform:
You shouldn't have any issues using it the way you are doing it already, other than configuration problems of your actual Redis Server.
// Where 192.168.0.1 is the hostname/IP of your Redis server
var pcm = new PooledRedisClientManager(new[] {"192.168.0.1:6379"});
var client = pcm.GetCacheClient();
Using ServiceStack.Redis with the ServiceStack platform:
Configure the PooledRedisClientManager
to be injected:
Assuming you are using ServiceStack's standard approach of dependancy injection, you will want to wire up your App Configuration to inject the client manager into your Service
base class. This makes Redis available to each request. So in your AppHost Configuration:
// Where 192.168.0.1 is the hostname/IP of your Redis server
container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("192.168.0.1:6379"));
// Register Redis cache client to be injected as ICacheClient, using pooled manager, registered above
container.Register<ICacheClient>(c => c.Resolve<IRedisClientsManager>().GetCacheClient());
Using Redis:
Once you have injected the pool, the standard ICacheClient
and IRedisClient
will be automatically wired up when you call base.Cache
or base.Redis
, respectively, within your Service
. So there is no further configuration required to use Redis.