之前写了Redis在Windwos下的安装方法,没有安装的朋友可以参考下
Windows下安装Redis
下面开始介绍Redis在C#中的使用
打开VS新建一个winform项目(我这里用的是vs2015)
然后添加NuGet包 StackExchange.Redis(这里注意要求.net .net framework的环境最少是4.5)
否则会报错
这时候我们把.net framework改为4.5
然后安装
安装好后我们在项目中添加一个helper类
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
namespace Redis
{
public static class RedisHelper
{
private static string Constr = "";
private static object _locker = new Object();
private static ConnectionMultiplexer _instance = null;
///
/// 使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer 断开连接,便可以初始化新的连接实例。
///
public static ConnectionMultiplexer Instance
{
get
{
if (Constr.Length == 0)
{
throw new Exception("连接字符串未设置!");
}
if (_instance == null)
{
lock (_locker)
{
if (_instance == null || !_instance.IsConnected)
{
_instance = ConnectionMultiplexer.Connect(Constr);
}
}
}
//注册如下事件
_instance.ConnectionFailed += MuxerConnectionFailed;
_instance.ConnectionRestored += MuxerConnectionRestored;
_instance.ErrorMessage += MuxerErrorMessage;
_instance.ConfigurationChanged += MuxerConfigurationChanged;
_instance.HashSlotMoved += MuxerHashSlotMoved;
_instance.InternalError += MuxerInternalError;
return _instance;
}
}
static RedisHelper()
{
}
public static void SetCon(string config)
{
Constr = config;
}
///
///
///
///
public static IDatabase GetDatabase()
{
return Instance.GetDatabase();
}
///
/// 这里的 MergeKey 用来拼接 Key 的前缀,具体不同的业务模块使用不同的前缀。
///
///
///
private static string MergeKey(string key)
{
return key;
//return BaseSystemInfo.SystemCode + key;
}
///
/// 根据key获取缓存对象
///
///
///
///
public static T Get(string key)
{
key = MergeKey(key);
return Deserialize(GetDatabase().StringGet(key));
}
///
/// 根据key获取缓存对象
///
///
///
public static object Get(string key)
{
key = MergeKey(key);
return Deserialize
然后设计窗口,这里就不多说了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Redis
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
try
{
string redisconf = "127.0.0.1:6379,password=123456,DefaultDatabase=0";
RedisHelper.SetCon(redisconf);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == "" || textBox2.Text == null)
{
textBox1.Text = "请填写键";
return;
}
if (textBox3.Text == "" || textBox3.Text == null)
{
textBox1.Text = "请填写值";
return;
}
if (textBox4.Text == "" || textBox4.Text == null)
{
textBox1.Text = "请填写过期时间";
return;
}
//设置
RedisHelper.Set(textBox2.Text, textBox3.Text, int.Parse(textBox4.Text));//键,值,过期时间
textBox1.Text = "添加成功";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text != null && textBox2.Text != "")
{
if (RedisHelper.Exists(textBox2.Text))
{
//读取
textBox1.Text = RedisHelper.Get(textBox2.Text).ToString();
}
else
{
textBox1.Text = "已过期或不存在";
}
}
else {
textBox1.Text = "请填写键";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Text != null && textBox2.Text != "")
{
if (RedisHelper.Exists(textBox2.Text))
{
//删除
RedisHelper.Remove(textBox2.Text).ToString();
textBox1.Text = "删除成功";
}
else
{
textBox1.Text = "已过期或不存在";
}
}
else
{
textBox1.Text = "请填写键";
}
}
private void TextBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
}
Demo下载