新聞中心
一、在windows中安裝redis數(shù)據(jù)庫
1、在Windows上安裝Redis,安裝的時候把加入到環(huán)境變量勾選上。
2、安裝好后,win+r打開運(yùn)行輸入cmd進(jìn)入控制臺程序,直接輸入redis-cli并且輸入ping,回復(fù)pong則鏈接成功,

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站制作、臨河網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、臨河網(wǎng)絡(luò)營銷、臨河企業(yè)策劃、臨河品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供臨河建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
1、簡單字符串存取
set key value
get key
···
3、安裝RedisStudio,redis管理界面,個人覺得最好使用的。
二、在ubuntu中安裝redis數(shù)據(jù)庫
1、在Vmware中安裝Ubuntu虛擬機(jī);
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下的文件全部拷貝進(jìn)該redis文件夾下
//如果沒有安裝gcc
sudo apt-get install gcc
//進(jìn)入redis文件夾執(zhí)行make
sudo make
sudo make install
4、此時就將redis安裝到了/usr/local/bin(服務(wù)端,客戶端都在里面啟動)下了
//進(jìn)入/usr/local/bin文件夾中
cd /usr/local/bin
//啟動redis-server
redis-server ~/redis/redis.conf
redis-cli
5、為了能在我們的win中訪問linux下的redis,我們還需要對redis.conf進(jìn)行少量的更改
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 * 創(chuàng)建時間:2017/5/8 9:12:47 * 當(dāng)前版本: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"]
///
/// 存下實(shí)體
///
///
/// 鍵
/// 實(shí)體
/// 過期時間
///
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);
}
}
///
/// 根據(jù)鍵來獲取實(shí)體
///
///
/// 鍵
///
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;
}
}
///
/// 批量存值
///
/// 鍵數(shù)組
/// 值數(shù)組
///
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);
}
}
///
/// 批量獲取值
///
/// 鍵數(shù)組
///
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;
}
}
///
/// 刪除鍵,即把這條數(shù)據(jù)刪除
///
/// 鍵
///
public static bool DeleteKey(string key) {
using (var client = ConnectionMultiplexer.Connect(_conn))
{
return client.GetDatabase().KeyDelete(key);
}
}
}
}
2、使用
/**************************************************************** * 作 者:xuxuzhaozhao * CLR 版本:4.0.30319.42000 * 創(chuàng)建時間:2017/5/8 10:30:26 * 當(dāng)前版本: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("請輸入想要對實(shí)體進(jìn)行的操作:");
Console.WriteLine("1、將實(shí)體加入Redis;");
Console.WriteLine("2、從Redis查詢實(shí)體;");
Console.WriteLine("3、從Redis更改實(shí)體;");
Console.WriteLine("4、從Redis刪除實(shí)體;");
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("實(shí)體成功加入Redis!過期時間為30分鐘!");
else
Console.WriteLine("加入失??!");
break;
case "2":
Console.WriteLine("請輸入要查詢值對應(yīng)的鍵:");
var key2 = Console.ReadLine();
LoginUser user = SERedisHelper.GetEntityBykey(key2);
if (user != null)
{
Console.WriteLine("查詢成功!實(shí)體信息如下:");
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(user));
}
else
Console.WriteLine("沒有查詢到{0}對應(yīng)的實(shí)體!",key2);
break;
case "3":
Console.WriteLine("請輸入要更改的鍵:");
var key3 = Console.ReadLine();
if(SERedisHelper.Set(key3,updateUser,new TimeSpan(0, 0, 30, 0)))
Console.WriteLine("實(shí)體更新成功!");
else
Console.WriteLine("更新失敗!");
break;
case "4":
Console.WriteLine("請輸入要刪除實(shí)體對應(yīng)的鍵:");
var key4 = Console.ReadLine();
if (SERedisHelper.DeleteKey(key4))
Console.WriteLine("實(shí)體刪除成功!");
else
Console.WriteLine("刪除失??!");
break;
default:
Console.WriteLine("請輸入數(shù)字進(jìn)行操作!");
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可以到主機(jī)寶貝資源站下載:
具體下載目錄在 /2018年資料/2月/14日/Windows上使用C#訪問Ubuntu上的Redis數(shù)據(jù)庫/
文章標(biāo)題:Windows上使用C#訪問Ubuntu上的Redis數(shù)據(jù)庫
當(dāng)前地址:http://fisionsoft.com.cn/article/dhidgsh.html


咨詢
建站咨詢
