Redis使用方法介绍

转载:https://blog.csdn.net/eff666/article/details/52998688?locationNum=3&fps=1点击打开链接

1、Redis介绍 

Redis——REmote DIctionary Server,可以直接理解为远程字典服务,也就是基于Key-Value模式Memcached+Database Persistence。Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。 
它通常被称为数据结构服务器,因为值(value)可以是字符串(String), 哈希(Map), 列表(list), 集合(sets)和有序集合(sorted sets)等类型。

简单来说,Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在Java中的redis- client。

2、Redis与其他 key - value缓存特点 
(1)Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。 
(2)Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。 
(3)Redis支持数据的备份,即master-slave模式的数据备份。

3、Java如何使用redis 
目前Redis大概有3中基于Java语言的Client:Jredis、Jedis和Redis4J。这里只说Jedis,因为它是官方提供的唯一Redis Client For Java Provider!

4、简单使用redis 
4.1 Maven Pom.xml配置

<dependency>  
    <groupId>redis.clients</groupId>  
    <artifactId>jedis</artifactId>  
    <version>2.8.1</version>  
    <type>jar</type>  
    <scope>compile</scope>  
</dependency> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.2 Jedis使用commons-pool完成池化实现

#properties配置
#最大分配的对象数
redis.pool.maxTotal=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWaitMillis=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true

#IP
redis.ip=xxxx
#Port
redis.port=6379
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4.3 初始化及其使用

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

import java.util.ResourceBundle;

public class Redis {
    private static JedisPool pool;
    static {
        //读取配置文件
        ResourceBundle bundle = ResourceBundle.getBundle(("redis"));

        if (bundle == null) {
            throw new IllegalArgumentException(
                    "[redis.properties] is not found!");
        }
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal")));
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
        config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWaitMillis")));
        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));

        pool = new JedisPool(config, bundle.getString("redis.ip"),
                Integer.valueOf(bundle.getString("redis.port")), 60000);
    }

    public static void main(String[] args){

        // 从池中获取一个Jedis对象
        Jedis jedis = pool.getResource();
        //System.out.println(jedis.get("redis.pool.maxTotal"));
        System.out.println(jedis);

        // 释放对象池
        //切记,最后使用后,释放Jedis对象
        //pool.returnResource(jedis); 高版本中官方废弃了此方法,可用如下方法释放
        try {
            jedis = pool.getResource();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39949109/article/details/80837628