Redis服务端的搭建(初级)

    前方低能,仅适合入门级菜鸟阅读,大神大牛通通闪开!


    前言:redis经常被用来做缓存(原因自行科普),基于学习的需要自己搭建了一个redis服务器,考虑到项目的分布式部署,所以前期开始的时候,redis直接就部署在服务端了(如果项目没有分布式部署,那么redis和站点在同一主机上性能更好哦)

    服务器环境:Linux-Centos7

    Redis版本: redis-3.2.11

    安装教程:https://blog.csdn.net/hp020740426/article/details/78791047 (小白系列,mkdir 新建文件夹 vim编辑保存退出等命令自行科普)

    使用教程:https://www.cnblogs.com/Lulus/p/7877688.html

    dll文件下载地址:https://files.cnblogs.com/files/sunshine-wy/StackExchange.Redis.rar  (博客园可以存少量文件,案例里需要引用一下这个dll,CSDN上也有很多下载源,要积分。GitHub也有,不过点我这链接下载是最省事的 哈哈)


    代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using webapp;
using Newtonsoft.Json;
using System.Data;
using System.Data.OleDb;
using StackExchange.Redis;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            RedisHelper redisHelper = new RedisHelper("127.0.0.1:6379");
            string value = "添加一条测试数据!2018年6月15日14:13:49";
            redisHelper.SetValue("Test01", value);
        }
 
    }

    public class RedisHelper
    {
        private ConnectionMultiplexer redis { get; set; }
        private IDatabase db { get; set; }
        public RedisHelper(string connection)
        {
            redis = ConnectionMultiplexer.Connect(connection);
            db = redis.GetDatabase();
        }
        public bool SetValue(string key, string value)
        {
            return db.StringSet(key, value);
        }
        public string GetValue(string key)
        {
            return db.StringGet(key);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool DeleteKey(string key)
        {
            return db.KeyDelete(key);
        }
    }
}

    以上是redis服务器端的搭建,以及本地网站的调用。后面会深入一点去学习redis持久化、集群的部署以及使用等等。敬请期待!

猜你喜欢

转载自www.cnblogs.com/sunshine-wy/p/9187416.html