Redis入门到精通只需要三篇博客

本文介绍了如何在Windows系统上安装Redis,并提供了简单的使用指南及性能优化建议,包括通过连接池提升效率的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(Redis介绍:略)

Redis-win-x64位程序的下载地址(缺分,分多的可以给我贡献点):

http://download.csdn.net/download/qq_33601179/10165429

linux下的安装百度一大堆,也不贴出来了,毕竟我没用过,随便贴一篇也不太好。

(1)win下安装redis

非常简单,只需要cmd命令行工具运行代码,吧服务程序跑起来就OK,代码为:(亲测有效)

redis-server.exe redis.windows.conf

具体的安装步骤,请看博客:

http://blog.csdn.net/jinwufeiyang/article/details/52156817

(2)简单使用Redis

这个我也不废话,直接看下面这个博客:(亲测有效)

http://www.runoob.com/redis/redis-java.html

(3)性能优化

Redis连接池,Redis本身就自带连接池。(亲测有效)

推荐将所需要的配置参数写到配置文件中,下面附上我封装的一个Redis连接池的类,由三个部分组成:类BaseRedis(使用Redis连接池:JedisPool)、类PropertiesUtils(用于读取配置文件)、redis.properties(参数配置文件)

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by Admin on 2017/12/19.
 */
public class BaseRedis {

    private static JedisPool pool;//redis连接池

    /**
     * 从连接池中获取对象
     *
     * @return
     */
    public static Jedis getJedis() {
        /**
         * 判定连接池是否已经初始化
         */
        if (pool == null) {
            PropertiesUtils propertiesUtils=new PropertiesUtils("redis.properties");
            /**
             * 从配置文件中读取配置参数
             */
            Integer maxTotal=propertiesUtils.getValueForInt("maxTotal");
            if(maxTotal==null){
                maxTotal=100;
            }

            Integer maxWaitMillis=propertiesUtils.getValueForInt("maxWaitMillis");
            if(maxWaitMillis==null){
                maxWaitMillis=1000;
            }


            Integer maxIdle=propertiesUtils.getValueForInt("maxIdle");
            if(maxIdle==null){
                maxIdle=10;
            }


            Integer port=propertiesUtils.getValueForInt("port");
            if(port==null){
                port=6379;
            }

            String redisUrl=propertiesUtils.getValue("redisUrl");
            if(redisUrl==null){
                redisUrl="localhost";
            }

            // 建立连接池配置参数
            JedisPoolConfig config = new JedisPoolConfig();
            // 设置最大连接数
            config.setMaxTotal(maxTotal);
            // 设置最大阻塞时间,记住是毫秒数milliseconds
            config.setMaxWaitMillis(maxWaitMillis);
            // 设置空间连接
            config.setMaxIdle(maxIdle);
            // 创建连接池
            pool = new JedisPool(config, redisUrl, port);
        }
        return pool.getResource();
    }


}
import java.io.InputStream;
import java.util.*;

/**
 * 读取配置文件中的属性
 */
public class PropertiesUtils {
    private String filePath;

    /**
     * 构造函数需要传入待读取的配置文件的名称
     *
     * @param propertiesFilePath
     */
    public PropertiesUtils(String propertiesFilePath) {
        // ../../这个是根据当前类所在的目录相对定位到配置文件的路径,具体按需修改
        this.filePath = "../../../../" + propertiesFilePath;
    }

    /**
     * 读取指定的配置参数
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        String str = "";
        try {
            Properties pro = new Properties();
            InputStream ins = this.getClass().getResourceAsStream(filePath);
            pro.load(ins);
            str = pro.getProperty(key);
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 读取指定的配置参数
     *
     * @param key
     * @return
     */
    public Integer getValueForInt(String key) {
        String str = getValue(key);
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取所有的配置参数
     *
     * @return
     */
    public Map<String, String> getAllValue() {
        //读取属性文件a.properties
        Map<String, String> map = new HashMap<String, String>();
        try {
            Properties prop = new Properties();
            InputStream ins = this.getClass().getResourceAsStream(filePath);
            prop.load(ins);     ///加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = prop.getProperty(key);
                map.put(key, value);
            }
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
#redis配置文件
#URL
redisUrl=localhost
#端口号
port=6379
#最大连接数
maxTotal=100
#超时时间,单位毫秒
maxWaitMillis=1000
#最大xxx
maxIdle=10

### Redis入门教程与进阶指南 #### 一、Redis简介 Redis(Remote Dictionary Server)是一种高性能的开源内存数据存储系统,广泛应用于数据库、缓存和消息队列等领域。它的主要特点包括速度极快、支持多种数据结构以及易于部署和使用[^1]。 #### 二、环境搭建 为了开始学习Redis,首先需要安装Redis服务端软件。以下是基本的安装步骤: - 下载最新版本的Redis源码包。 - 编译并安装Redis服务器及其客户端工具集。 完成安装后,可以通过启动`redis-server`来运行Redis实例,并通过`redis-cli`连接到该实例进行交互测试[^2]。 #### 三、基础功能介绍 Redis的核心优势在于其对不同数据类型的强大支持能力,其中包括但不限于字符串(Strings),哈希(Hashes),列表(Lists),集合(Sets)及有序集合(Sorted Sets)[^1]。每种数据类型都有特定的操作命令用于增删改查等常规管理活动。 #### 四、持久化机制详解——BGSAVE过程解析 当触发后台保存(`bgsave`)操作时,Redis会创建一个新的子进程专门用来把当前内存里的全部数据序列化成RDB文件形式落地磁盘之上;此过程中原主线程依旧能够正常响应来自外部的各种请求而不受影响。具体流程如下所示: 1. Fork出一个子进程; 2. 子进程中将所有的key-value pair依次写入至临时文件当中去; 3. 当所有数据均已完成拷贝之后,则正式替换掉原有的rdb snapshot文档位置; 4. 同步更新相关元信息比如时间戳等内容以便后续查询统计用途等等[^3]。 #### 五、高级特性之一:SORT命令定制排序逻辑 除了常见的CRUD之外,还有许多实用的功能可以帮助我们更灵活地操控我们的数据集。例如sort指令允许按照自定义规则重新排列list,set 或者sorted set里面的成员项顺序。其中by选项特别值得关注因为它可以让程序根据另外关联起来的一个field value来进行最终决定排名先后次序而不是单纯依赖于原始item本身所携带的信息量大小关系。举个例子来说就是如果我们有一个用户表单记录了每个人的年龄属性那么就可以轻松实现按年龄段分组展示效果啦[^4]! ```python import redis # 创建连接池对象 pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True) # 获取StrictRedis实例 client = redis.StrictRedis(connection_pool=pool) # 假设已经存在名为myset的数据集合 result = client.sort('myset', by="weights_*->age", alpha=False) print(result) ``` 以上代码片段展示了如何利用Python脚本调用redis-py库接口执行带条件约束的排序运算任务。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值