Windows上使用C#访问Ubuntu上的Redis数据库

一、在windows中安装redis数据库

1、在Windows上安装Redis,安装的时候把加入到环境变量勾选上。
2、安装好后,win+r打开运行输入cmd进入控制台程序,直接输入redis-cli并且输入ping,回复pong则链接成功,

创新互联专注为客户提供全方位的互联网综合服务,包含不限于做网站、网站制作、临河网络推广、重庆小程序开发、临河网络营销、临河企业策划、临河品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供临河建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

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可以到主机宝贝资源站下载:

具体下载目录在 /2018年资料/2月/14日/Windows上使用C#访问Ubuntu上的Redis数据库/

当前题目:Windows上使用C#访问Ubuntu上的Redis数据库
转载注明:http://www.hantingmc.com/qtweb/news14/64564.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联