Redis 初学(1)

1、打开redis服务器,默认端口为6379
2、maven导入:
  <!--  Redis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.9.0</version>
    </dependency>

  <!-- Test -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.3.0</version>
        <scope>test</scope>
    </dependency>
3、设置JedisPoolConfig配置类,根据配置类创建连接池,从连接池种获取一个jedis对象,
对该对象进行操作,最后关闭连接。
        //1)	创建连接池配置对象,设置最大连接数10,设置用户最大等待时间2000毫秒
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10);
        config.setMaxWaitMillis(2000);
        //2)	通过配置对象做为参数,创建连接池对象
        JedisPool pool = new JedisPool(config, "localhost", 6379);
        //3)	从连接池里面获取jedis连接对象,执行redis命令。
        Jedis jedis = pool.getResource();
        //4)	执行redis命令sadd写入set集合类型的数据:students=白骨精,孙悟空,猪八戒
        jedis.sadd("students", "aaa", "bbb", "ccc");
        //5)	执行redis命令smembers读取集合中的数据
        Set<String> students = jedis.smembers("students");
        //6)	输出读取的数据
        System.out.println(students);
        //7)	关闭连接对象(通常连接池不关闭)
        jedis.close();
        pool.close();

相关配置

1、java.util.ResourceBundle类是专门用于:读取类路径下Properties配置文件的类
Jedis.perpoties
#连接IP地址
redis.host=127.0.0.1
#端口号
redis.port=6379
#密码
redis.password=123456
#使用的数据库
redis.database=4
#超时时间
redis.timeout=10000
#最大空闲数
redis.maxIdle=300
#最大空连接数
redis.maxTotal=10
#最大等待时间
redis.maxWaitMillis=30
#获取连接时,检测连接是否成功
redis.testOnBorrow=true
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
//得到配置文件中的属性值
String host = bundle.getString("host");
int port = Integer.parseInt(bundle.getString("port"));
int maxTotal = Integer.parseInt(bundle.getString("maxTotal"));
int maxWaitMillis = Integer.parseInt(bundle.getString("maxWaitMillis"));

连接池工具类:

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

import java.util.ResourceBundle;

/**
 * 连接池工具类
 */
public class JedisUtils {

    //创建一个连接对象
    private static JedisPool pool;

    static {
        //创建连接池的配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        //设置最大连接数和最长等待时间
        ResourceBundle bundle = ResourceBundle.getBundle("jedis");
        //得到配置文件中的属性值
        String host = bundle.getString("host");
        int port = Integer.parseInt(bundle.getString("port"));
        int maxTotal = Integer.parseInt(bundle.getString("maxTotal"));
        int maxWaitMillis = Integer.parseInt(bundle.getString("maxWaitMillis"));
        //设置配置对象的参数
        config.setMaxTotal(maxTotal);
        config.setMaxWaitMillis(maxWaitMillis);
        //创建连接池对象
        pool = new JedisPool(config, host, port);
    }

    /**
     * 得到redis连接对象
     * @return
     */
    public static Jedis getJedis() {
        return pool.getResource();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值