application-local.yml

spring:
  datasource:
    name: datasource
    druid:
      url: jdbc:mysql://localhost:3306/joy?useSSL=false&characterEncoding=UTF-8&serverTimezone=CTT&useAffectedRows=true&allowPublicKeyRetrieval=true
      username: test
      password: 123456
      name: my
      driver-class-name: com.mysql.cj.jdbc.Driver
      initial-size: 1
      min-idle: 1
      max-active: 20
      max-wait: 60000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20
  redis:
    jedis:
      pool:
        max-active: 8
        min-idle: 2
        max-idle: 8
        max-wait: -1
    database:
      base: 0
      order: 8
    host: 111.111.111.111
    port: 6379
    password: redis123456
    timeout: 5000
  cache:
    redis:
      time-to-live: 360

RedisServiceImpl.java

package com.huixiaoer.joy.api.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * redis
 * @author wang
 */
@Service
@Slf4j
public class RedisServiceImpl {
    @Autowired
    JedisPool pool;

    @Value("${spring.redis.database.order}")
    private Integer orderDBIndex;

    /**
     * 同步获取Jedis实例
     *
     * @return Jedis
     */
    public synchronized  Jedis getCache() {
        Jedis connection = pool.getResource();
        connection.select(orderDBIndex);
        return connection;
    }

    /**
     * 根据key 获取对象
     *
     * @param key
     * @return
     */
    public  String get(String key) {
        Jedis connection = getCache();
        try {
            return connection.get(key);
        } finally {
            releaseConnection(connection);
        }
    }


    /**
     * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
     *
     * @param key
     *            rediskey
     * @param value
     *            如果是不是String转化为json
     * @return OK
     * @throws Exception
     */
    public  String set(String key, Object value) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.set(key, (String) value);
            }

            String json = JSONObject.toJSONString(value);
            return connection.set(key, json);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 校验key存在
     * @param key
     * @return
     */
    public  Boolean exists(String key) {
        Jedis connection = getCache();
        try {
            return connection.exists(key);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public  String set(String key, Object value, Integer expire) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.setex(key, expire, (String) value);
            }
            String json = JSONObject.toJSONString(value);
            return connection.setex(key, expire, json);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 删除缓存中得对象,根据key
     *
     * @param key
     * @return
     */
    public  Long del(String key) {
        Jedis connection = getCache();
        try {
            return connection.del(key);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     *
     * Description: 获取key的过期时间
     *
     * @param key
     * @return
     * @return long 剩余过期时间(秒)
     * @throw
     * @Author dongwang34@creditease.cn Create Date: 2015-11-30
     */
    public  long ttl(String key) {
        Jedis connection = getCache();
        try {
            return connection.ttl(key);
        } finally {
            releaseConnection(connection);
        }
    }


    /** key **/
    /**
     * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
     *
     * @param key
     *            rediskey
     * @param expire
     *            seconds
     * @return 1
     * @throws Exception
     */
    public  Long expire(String key, Integer expire) {
        Jedis connection = getCache();
        try {
            return connection.expire(key, expire);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 根据key 获取对象集合
     *
     * @param key
     * @param clazz
     *            集合元素泛型
     * @return
     */
    public  <T> T get(String key, Class<T> clazz) {
        Jedis connection = getCache();
        try {
            return (T) JSONObject.parseObject(
                    connection.get(key), clazz);
        } finally {
            releaseConnection(connection);
        }

    }

    /**
     * 右进
     * @param key
     * @param value
     * @return
     */
    public  Long rpush(String key, Object value) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.rpush(key, (String) value);
            }
            String json = JSONObject.toJSONString(value);
            return connection.rpush(key, json);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 右出
     * @param key
     * @return
     */
    public  String rpop(String key) {
        Jedis connection = getCache();
        try {
            return connection.rpop(key);
        } finally {
            releaseConnection(connection);
        }
    }

    public Long llen(String key){
        Jedis connection = getCache();
        try {
            return connection.llen(key);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 释放连接
     * @param connection
     */
    private  void releaseConnection(Jedis connection) {
        if (connection != null) {
            connection.close();
        }
    }

    public  Long lpush(String key, Object value) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.lpush(key, (String) value);
            }
            String json = JSONObject.toJSONString(value);
            return connection.lpush(key, json);
        } finally {
            releaseConnection(connection);
        }
    }
}

 

BaseServiceImpl.java

package com.huixiaoer.joy.api.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * redis
 * @author wang
 */
@Service
@Slf4j
public class BaseRedisServiceImpl {
    @Autowired
    JedisPool pool;

    @Value("${spring.redis.database.base}")
    private Integer baseDBIndex;

    /**
     * 同步获取Jedis实例
     *
     * @return Jedis
     */
    public synchronized  Jedis getCache() {
        Jedis connection = pool.getResource();
        connection.select(baseDBIndex);
        return connection;
    }

    /**
     * 根据key 获取对象
     *
     * @param key
     * @return
     */
    public  String get(String key) {
        Jedis connection = getCache();
        try {
            return connection.get(key);
        } finally {
            releaseConnection(connection);
        }
    }


    /**
     * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
     *
     * @param key
     *            rediskey
     * @param value
     *            如果是不是String转化为json
     * @return OK
     * @throws Exception
     */
    public  String set(String key, Object value) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.set(key, (String) value);
            }

            String json = JSONObject.toJSONString(value);
            return connection.set(key, json);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 校验key存在
     * @param key
     * @return
     */
    public  Boolean exists(String key) {
        Jedis connection = getCache();
        try {
            return connection.exists(key);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public  String set(String key, Object value, Integer expire) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.setex(key, expire, (String) value);
            }
            String json = JSONObject.toJSONString(value);
            return connection.setex(key, expire, json);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 删除缓存中得对象,根据key
     *
     * @param key
     * @return
     */
    public  Long del(String key) {
        Jedis connection = getCache();
        try {
            return connection.del(key);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     *
     * Description: 获取key的过期时间
     *
     * @param key
     * @return
     * @return long 剩余过期时间(秒)
     * @throw
     * @Author dongwang34@creditease.cn Create Date: 2015-11-30
     */
    public  long ttl(String key) {
        Jedis connection = getCache();
        try {
            return connection.ttl(key);
        } finally {
            releaseConnection(connection);
        }
    }


    /** key **/
    /**
     * 如果 key 已经持有其他值, SET 就覆写旧值,无视类型
     *
     * @param key
     *            rediskey
     * @param expire
     *            seconds
     * @return 1
     * @throws Exception
     */
    public  Long expire(String key, Integer expire) {
        Jedis connection = getCache();
        try {
            return connection.expire(key, expire);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 根据key 获取对象集合
     *
     * @param key
     * @param clazz
     *            集合元素泛型
     * @return
     */
    public  <T> T get(String key, Class<T> clazz) {
        Jedis connection = getCache();
        try {
            return (T) JSONObject.parseObject(
                    connection.get(key), clazz);
        } finally {
            releaseConnection(connection);
        }

    }

    /**
     * 右进
     * @param key
     * @param value
     * @return
     */
    public  Long rpush(String key, Object value) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.rpush(key, (String) value);
            }
            String json = JSONObject.toJSONString(value);
            return connection.rpush(key, json);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 右出
     * @param key
     * @return
     */
    public  String rpop(String key) {
        Jedis connection = getCache();
        try {
            return connection.rpop(key);
        } finally {
            releaseConnection(connection);
        }
    }

    public Long llen(String key){
        Jedis connection = getCache();
        try {
            return connection.llen(key);
        } finally {
            releaseConnection(connection);
        }
    }

    /**
     * 释放连接
     * @param connection
     */
    private  void releaseConnection(Jedis connection) {
        if (connection != null) {
            connection.close();
        }
    }

    public  Long lpush(String key, Object value) {
        Jedis connection = getCache();
        try {
            if (value instanceof String) {
                return connection.lpush(key, (String) value);
            }
            String json = JSONObject.toJSONString(value);
            return connection.lpush(key, json);
        } finally {
            releaseConnection(connection);
        }
    }
}

 

结束