Redis使用方法介绍

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> 

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

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();
            }
        }
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值