本篇讨论下如何在 Windows 平台上安装 Redis,安装包可以到 GitHub:https://github.com/MSOpenTech/redis/releases 去下载,在安装的过程中,记得勾选一下 add Redis to the PATH environmental variable,将 Redis的路径添加到环境变量中,等到安装程序执行完毕之后,可以通过 Run -> service.msc 到 windows 的服务面板去看下 redis service 是否已经安装成功。
privatestaticstringGet(string host, string key) { using (RedisClient redisClient = new RedisClient(host)) { return redisClient.Get<string>(key); } }
注意 RedisClient 的 Set 和 Get 方法是如何从 Redis 中发送和读取数据的,我准备留给你一件事情,你可以试着修改一下这两个方法让其支持泛型,这样的话你的两个方法就可以支持任何类型了,对吧。
接下来看一下如何在 Main 方法中调用,代码如下:
staticvoidMain(string[] args) { string host = "localhost"; string key = "IDG"; // Store data in the cache bool success = Save(host, key, "Hello World!"); // Retrieve data from the cache using the key Console.WriteLine("Data retrieved from Redis Cache: " + Get(host,key)); Console.Read(); }