一、在 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
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
var jsonstr = Newtonsoft.Json.JsonConvert.SerializeObject(t);
return client.GetDatabase().StringSet(key, jsonstr, ts);
}
}
///
///
///
/// 鍵
///
public static T GetEntityBykey
{
using (var client = ConnectionMultiplexer.Connect(_conn))
{
var jsonstr = client.GetDatabase().StringGet(key);
return string.IsNullOrEmpty(jsonstr) ? null : Newtonsoft.Json.JsonConvert.DeserializeObject
}
}
///
///
/// 鍵
/// 值
/// 過期時間
///
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
for (int i = 0; i < count; i++)
{
keyValuePair[i] = new KeyValuePair
}
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
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 公社資源站下載: