zoukankan      html  css  js  c++  java
  • 煮酒论redis

    redis是一种性能极高的key-value数据库读的速度是110000次/s,写的速度是81000次/s 

    原理是将数据保存在内存中,作为缓存使用,避免了从数据库中直接拿取数据,适合处理系统的高并发。

    个人认为BS项目处理并发使用redis,CS项目处理并发使用多线程

    丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作

    Redis支持数据的备份,即master-slave模式的数据备份

    下载地址:https://github.com/MSOpenTech/redis/releases  支持Windows32位和64位,选择适合自己的版本

    进行解压

    在cmd命令中启动redis服务,出现这样的效果启动完成

    再打开一个cmd命令窗口,注意本窗口不要关闭  输入命令

    cd C:UsersAdministratorDesktop edisRedis3 打开地址
    C:UsersAdministratorDesktop edisRedis3>redis-cli 操作redis
    127.0.0.1:6379> auth redis 输入密码
    set asd 123456 赋值
    get asd 取值
    flushdb 清除当前redis数据库缓冲
    flushall 清除整个redis缓冲

    在项目中封装一个redishelper,注意配置文件的引用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ServiceStack.Redis;
    using Newtonsoft.Json;
    
    namespace DAL
    {
        public class RedisHelper
        {
            //key: admin(管理员), cate(图书类别),location(图书位置),book(图书),reader(读者),time(时间),record(图书借阅记录),circle(圈子)
            private static RedisClient Redis = null;
            static RedisHelper()
            {
                Redis = new RedisClient("127.0.0.1", 6379);
            }
            //存数据
            public int SetData(string key, string data, int hours = 5)
            {
                
                if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(data))
                {
                    return 0;
                }
                else
                {
                    DateTime expiryTime = DateTime.Now.AddHours(hours);
                    byte[] byteArray = System.Text.Encoding.Default.GetBytes(data);
                    Redis.Set(key, byteArray, expiryTime);
                    return 1;
                }
            }
            //取数据
            public string GetData(string key)
            {
    
                if (string.IsNullOrEmpty(key))
                {
                    return null;
                }
                else
                {
                    byte[] value = Redis.Get(key);
                    if (value == null)
                    {
                        return null;
                    }
                    else
                    {
                        return System.Text.Encoding.Default.GetString(value);
                    }
                }
    
    
    
            }
            //删除
            public void DelData(string key)
            {
                Redis.Del(key);
            }
        }
    }

    根据键值对存储对缓冲进行操作,存入key value,根据key值进行查找value

    缓冲雪崩 缓冲穿透 缓冲预热  缓冲更新 缓冲降级等问题正在学习

  • 相关阅读:
    三维dem
    geoserver 通过代码实现发布地图服务
    restful发布服务
    php防攻击
    redis主从复制
    redis虚拟内存
    redis 持久化
    lamp源码安装
    mysql优化
    php匿名函数与闭包函数
  • 原文地址:https://www.cnblogs.com/dujian123/p/10847972.html
Copyright © 2011-2022 走看看