一、在 Windows 中安装 redis 资料库
1 、在 Windows 上安装 Redis, 安装的时候把加入到环境变数勾选上。
2 、安装好后,win+r 开启执行输入 cmd 进入控制台程式,直接输入 redis-cli 并且输入 ping,回复 pong 则连结成功,
1 、简单字串存取
set key value
get key
···
3 、安装 RedisStudio,redis 管理介面,个人觉得最好使用的。
二、在 Ubuntu 中安装 redis 资料库
1 、在 Vmware 中安装 Ubuntu 虚拟机器;
2 、安装 redis
//下载 redis-3.2.6
sudo wget http://download.redis.io/releases/redis-3.2.6.tar.gz
//解压 redis-3.2.6
sudo tar -zxvf redis-3.2.6.tar.gz
3 、在~/下新建一个 redis 资料夹并且将 redis-3.2.6 下的档案全部拷贝进该 redis 资料夹下
//如果没有安装 gcc
sudo apt-get install gcc
//进入 redis 资料夹执行 make
sudo make
sudo make install
4 、此时就将 redis 安装到了/usr/local/bin(服务端,客户端都在里面启动)下了
//进入/usr/local/bin 资料夹中
cd /usr/local/bin
//启动 redis-server
redis-server ~/redis/redis.conf
redis-cli
5 、为了能在我们的 win 中访问 linux 下的 redis,我们还需要对 redis.conf 进行少量的更改
protected-mode yes –> protected-mode no
bind 127.0.0.1 –> #bind 127.0.0.1
6 、在终端输入 ifconfig,检视 ubuntu 的 IP 地址 inet addr:xxx.xxx.xxx.xxx
三、 C#使用 redis
1 、 Nuget 安装 StackExchange.Redis, 程式中加入 SeRedisHelper 类
/****************************************************************
* 作 者:xuxuzhaozhao
* CLR 版本:4.0.30319.42000
* 建立时间:2017/5/8 9:12:47
* 当前版本:1.0.0.1
*
* 描述说明: StackExchange.Redis 的帮助类
*
* 修改历史:
*
*****************************************************************/

using System;
using StackExchange.Redis;
using System.Configuration;
using System.Collections.Generic;

namespace Common.Redis
{
public class SERedisHelper
{
private static string _conn = ConfigurationManager.AppSettings[“RedisConnectString”]

///

/// 存下实体
///

///
/// 键 /// 实体 /// 过期时间 ///
public static bool Set(string key, T t, TimeSpan ts)
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
var jsonstr = Newtonsoft.Json.JsonConvert.SerializeObject(t);
return client.GetDatabase().StringSet(key, jsonstr, ts);
}
}

///

/// 根据键来获取实体
///

///
/// 键 ///
public static T GetEntityBykey(string key) where T : class
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
var jsonstr = client.GetDatabase().StringGet(key);
return string.IsNullOrEmpty(jsonstr) ? null : Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstr);
}
}

///

/// 存单个键值对
///

/// 键 /// 值 /// 过期时间 ///
public static bool StringSetSingle(string key, string value, TimeSpan ts)
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().StringSet(key, value, ts);
}
}

///

/// 取单个值
///

/// 键 ///
public static string StringGetSingle(string key)
{
try
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().StringGet(key);
}
}
catch (Exception)
{

return null;
}
}

///

/// 批量存值
///

/// 键阵列 /// 值阵列 ///
public static bool StringSetMany(string[] keys, string[] values)
{
var count = keys.Length;
var keyValuePair = new KeyValuePair[count];
for (int i = 0; i < count; i++) { keyValuePair[i] = new KeyValuePair(keys[i], keys[i]);
}
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().StringSet(keyValuePair);
}
}

///

/// 批量获取值
///

/// 键阵列 ///
public static string[] StringGetMany(string[] keysStrings)
{
var count = keysStrings.Length;
var keys = new RedisKey[count];
var values = new string[count];
for (int i = 0; i < count; i++) { keys[i] = keysStrings[i]; } try { using (var client = ConnectionMultiplexer.Connect(_conn)) { var valuess = client.GetDatabase().StringGet(keys); for (int i = 0; i < count; i++) { values[i] = valuess[i]; } return values; } } catch (Exception) { return null; } } ///

/// 删除键,即把这条资料删除
///

/// 键 ///
public static bool DeleteKey(string key)
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().KeyDelete(key);
}
}
}
}

2 、使用
/****************************************************************
* 作 者:xuxuzhaozhao
* CLR 版本:4.0.30319.42000
* 建立时间:2017/5/8 10:30:26
* 当前版本:1.0.0.1
*
* 描述说明:
*
* 修改历史:
*
*****************************************************************/

using System;
using Common.Redis;

namespace Redis.Test
{
class Program
{
static void Main(string[] args)
{
var loginUser = new LoginUser
{
Id = 1,
Name = “xuxuzhaozhao”,
Gender = true,
CreateTime = DateTime.Today,
Money = 12.12M
};
var updateUser = new LoginUser
{
Id = 2,
Name = “xuchengyi”,
Gender = false,
CreateTime = DateTime.Now,
Money = 19.92M
};

while (true)
{
Console.WriteLine();
Console.WriteLine(“========================================”);
Console.WriteLine(“ 请输入想要对实体进行的操作:”);
Console.WriteLine(“1 、将实体加入 Redis;”);
Console.WriteLine(“2 、从 Redis 查询实体;”);
Console.WriteLine(“3 、从 Redis 更改实体;”);
Console.WriteLine(“4 、从 Redis 删除实体;”);
Console.WriteLine(“=======================================”);
var op = Console.ReadLine();
switch (op)
{
case “1”:
Console.WriteLine(“ 请输入键名:”);
var key1 = Console.ReadLine();
if(SERedisHelper.Set(key1,loginUser,new TimeSpan(0,0,30,0)))
Console.WriteLine(“ 实体成功加入 Redis!过期时间为 30 分钟!”);
else
Console.WriteLine(“ 加入失败!”);
break;
case “2”:
Console.WriteLine(“ 请输入要查询值对应的键:”);
var key2 = Console.ReadLine();
LoginUser user = SERedisHelper.GetEntityBykey(key2);
if (user != null)
{
Console.WriteLine(“ 查询成功!实体资讯如下:”);
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(user));
}
else
Console.WriteLine(“ 没有查询到 {0} 对应的实体!”,key2);
break;
case “3”:
Console.WriteLine(“ 请输入要更改的键:”);
var key3 = Console.ReadLine();
if(SERedisHelper.Set(key3,updateUser,new TimeSpan(0, 0, 30, 0)))
Console.WriteLine(“ 实体更新成功!”);
else
Console.WriteLine(“ 更新失败!”);
break;
case “4”:
Console.WriteLine(“ 请输入要删除实体对应的键:”);
var key4 = Console.ReadLine();
if (SERedisHelper.DeleteKey(key4))
Console.WriteLine(“ 实体删除成功!”);
else
Console.WriteLine(“ 删除失败!”);
break;
default:
Console.WriteLine(“ 请输入数字进行操作!”);
break;
}
}
}
}

class LoginUser
{
public int Id { get; set; }
public string Name { get; set; }
public bool Gender { get; set; }
public DateTime CreateTime { get; set; }
public decimal Money { get; set; }
}
}
Redis 可以到 Linux 公社资源站下载: