java简单集成redis的使用。

版权声明:未经授权,不得转载。 https://blog.csdn.net/soulsda/article/details/86467114

         先说下,用的是redis.clients这个客户端,好处是,语句贴合原redis的命令,方便入门,另一种客户端spring-boot-starter-data-redis,可能以后会写,但语句有点怪,不适合入门。

1.引入jar包

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>

        //版本号似乎是自动跟随boot版本?如果没有 引入 自己加上 版本号为2.9.0

2.写一个redis的工厂类?  没太看懂  好像是 工厂 + 单例  

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;

@Component
@Slf4j
public class JedisPoolWrapper {


	private JedisPool jedisPool = null;
	
	@Autowired
	private Parameters parameters;
	
	@PostConstruct
	public void init() throws MyException {
		try {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(parameters.getRedisMaxTotal());
			config.setMaxIdle(parameters.getRedisMaxIdle());
			config.setMaxWaitMillis(parameters.getRedisMaxWaitMillis());

			jedisPool = new JedisPool(config,parameters.getRedisHost(),parameters.getRedisPort(),2000,parameters.getRedisAuth());
		} catch (Exception e) {
			log.error("Fail to initialize jedis pool", e);
			throw new MyException("Fail to initialize jedis pool");
		}
	}

	public JedisPool getJedisPool() {
		return jedisPool;
	}
	
}

3.用法

    @Autowired
	private JedisPoolWrapper jedisPoolWrapper;

   交给了spring管理,所以用的时候注入到类中就行了,前提是这个类也在spring下,也就是controller,service或者打了@Component注解中等由spring容器管理的类中。

简单几个例子

1.
            //获取redis对象 
            JedisPool pool = jedisPoolWrapper.getJedisPool();
            Jedis Jedis = pool.getResource()
			
		Jedis.select(0);//跳转到0片区
		Jedis.set(key, value);//设置key,value 没有过期时间

            //获取0片区 key 对应的value的值 
            Jedis.select(0);
	    value = Jedis.get(key);


         //设置成功,返回 1 。 设置失败,返回 0 。
         //返回一个long类型的数据
         //setnx和普通set区别是 如果redis已经存在名字相同的key则不设置 返回失败
	result = jedis.setnx(key, value); 
	jedis.expire(key, expiry); 设置某个key的 过期时间  单位 秒
/**
*  上面写的有点乱 见谅
*/


2.   具体实例
	public void cache(String key, String value) {
		try {
			JedisPool pool = jedisPoolWrapper.getJedisPool();
			if (pool != null) {
				try (Jedis Jedis = pool.getResource()) {
					Jedis.select(0);
					Jedis.set(key, value);
				}
			}
		} catch (Exception e) {
			log.error("Fail to cache value", e);
		}
	}
		

4.核心

  

  核心就是上面那个redis的连接工厂(是工厂吧,应该是吧。)
  里面用了一些别的技术
  比如:

  1.取配置文件数据
    @Autowired
    private Parameters parameters;
  

  2.自定义异常处理
    throw new MyException("Fail to initialize jedis pool");

  3.lombok插件 简化日志和实体类等简化jar包 
    @Slf4j  日志
    
 配置文件可以换成手动填写的 异常换成用的项目的异常处理 日志 把注解删掉换成对应项目日志就行
 不过应该boot项目 上面这些技术都会集成的 灵活用吧

5.redis配置

redis:
    host: 127.0.0.1
    port: 6379
    auth:
    max-idle: 5
    max-total: 10
    max-wait-millis: 3000

6.闲谈

   感觉jedis 这个客户端很贴合原版,非常适合入门 比较人性化  不过  都在说boot官方整合那个比较好   也不知道为什么 有机会写一下

猜你喜欢

转载自blog.csdn.net/soulsda/article/details/86467114