一、在win中安装redis数据库
1、在windows上安装Redis,由于博客园不支持上传msi文件,所以我把后缀改为了rar,下载下来的时候别忘了将rar改为msi,安装的时候把加入到环境变量勾选上。
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"]
/// <summary>
/// 存下实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">键</param>
/// <param name="t">实体</param>
/// <param name="ts">过期时间</param>
/// <returns></returns>
public static bool Set<T>(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);
}
}
/// <summary>
/// 根据键来获取实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">键</param>
/// <returns></returns>
public static T GetEntityBykey<T>(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<T>(jsonstr);
}
}
/// <summary>
/// 存单个键值对
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="ts">过期时间</param>
/// <returns></returns>
public static bool StringSetSingle(string key, string value, TimeSpan ts)
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().StringSet(key, value, ts);
}
}
/// <summary>
/// 取单个值
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static string StringGetSingle(string key)
{
try
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().StringGet(key);
}
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 批量存值
/// </summary>
/// <param name="keys">键数组</param>
/// <param name="values">值数组</param>
/// <returns></returns>
public static bool StringSetMany(string[] keys, string[] values)
{
var count = keys.Length;
var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count];
for (int i = 0; i < count; i++)
{
keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(keys[i], keys[i]);
}
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().StringSet(keyValuePair);
}
}
/// <summary>
/// 批量获取值
/// </summary>
/// <param name="keysStrings">键数组</param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 删除键,即把这条数据删除
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
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<LoginUser>(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; }
}
}