chaosxin 发表于 2015-7-23 08:51:17

c#连接Redis--windows下Redis的安装配置与存读取数据

  原文:http://blog.iyunv.com/zx13525079024/article/details/8124790
        
  Redis是一个不错的缓存数据库,读取数据速度效率都很不错。今天大家共同研究下redis的用法。结合网上的资料和自己的摸索,先来看下安装与配置把。
  咱们主要看在WINDOWS上怎样使用REDIS数据库。
     下载地址:https://github.com/dmajkic/redis/downloads
      1. 选择一个版本进行下载,压缩包中包括32位和64位的安装工具。我们这里使用32位的。
     下载解压后的文件如下图:

  2. 在D建立一个redis 文件夹(当然建在其他盘也可以),然后把上面解压的32bit文件夹下面的所有文件拷贝到redis文件夹里面.

  3.打开服务器端
     通过CMD命令行打开服务器端,首先通过命令行转到d:\redis文件夹,
     然后输入如下命令 redis-server.exe redis.conf

  4.打开客户端
     服务器端的CMD命令行不要关闭,再单独打开一个CMD命令行,切换到d:\redis文件夹,
     输入如下命令:redis-cli.exe -h 127.0.0.1 -p 6379
        
  然后输入 set pwd 123456
                    get pwd
      获取返回值成功,说明服务器端配置成功,

  以上是redis的安装与配置,欢迎大家交流
  
  这一节演示下载.NET中怎样使用Redis存储数据.在.net中比较常用的客户端类库是ServiceStack,看下通过servicestack怎样存储数据。
     DLL下载:https://github.com/ServiceStack/ServiceStack.Redis
  
     下载完成后,DLL中包括四个DLL文件,然后把这四个文件添加到自己的项目中。
  
     
  
      Redis中包括四种数据类型,Strings, Lists,Sets, Sorted Sets
  
      接下来我们一一看这四种类型的用法
  
     1.连接redis服务器
  






view plaincopyprint?

[*]RedisClient client;
[*]private void button1_Click(object sender, EventArgs e)
[*]{
[*]    //连接服务器   
[*]   client = new RedisClient("127.0.0.1",6379);
[*]}
  

      RedisClient client;
private void button1_Click(object sender, EventArgs e)
{
//连接服务器
client = new RedisClient("127.0.0.1",6379);
}

  
      2.Strings类型
         Strings能存储字符串,数字等类型,浮点型等.NET中的基本类型
  






view plaincopyprint?

[*]private void button2_Click(object sender, EventArgs e)
[*]   {
[*]         //存储用户名和密码   
[*]         client.Set("username","524300045@qq.com");
[*]         client.Set("pwd", 123456);
[*]         string username = client.Get("username");
[*]         int pwd=client.Get("pwd");
[*]
[*]         client.Set("price", 12.10M);
[*]         decimal price = client.Get("price");
[*]         MessageBox.Show(price.ToString());
[*]
[*]         MessageBox.Show("用户名:"+username+",密码:"+pwd.ToString());
[*]   }
  

   private void button2_Click(object sender, EventArgs e)
{
//存储用户名和密码
client.Set("username","524300045@qq.com");
client.Set("pwd", 123456);
string username = client.Get("username");
int pwd=client.Get("pwd");
client.Set("price", 12.10M);
decimal price = client.Get("price");
MessageBox.Show(price.ToString());
MessageBox.Show("用户名:"+username+",密码:"+pwd.ToString());
}
  
  
  
页: [1]
查看完整版本: c#连接Redis--windows下Redis的安装配置与存读取数据