springBoot 使用自定义配置文件集成 redis

SpringBoot中集成Redis 有了,并于Spring Boot配置文件中定义Redis相关属性后,就会在项目启动时会注入到Redis中去。前面说到spring中为redis定义了专门的自动配置类,其中定义创建泛型为的RedisTemplate对象,为了允许自定义配置Bean,自动配置类使用@ConditionalOnMissingBean注解,表示当其他地方定义一个RedisTemplate的Bean时,会替代自动配置中的结果。​@Bean​@Bean复制代码为了使用操作更方便,我们定义一个。 阅读详情

springBoot用官方的那种方法集成redis比较简单,今天大佬非要让用自定义配置文件去集成redis,这样子反而变的麻烦了点,甚至有点违背springboot的原则了;

依赖jar:

只贴了redis的;

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

这里我采用的不是写对应配置config用注解的那种方式去获取配置文件的值,还是采用的需要时去读取配置文件:


import com.sess.june17th.common.util.PropertiesHelper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
  * @Description: 读取redis配置文件
  * @param ${tags}
  * @return ${return_type}
  * @throws
  * @author liuxue
  * @date 2019/7/31 0031 下午 5:03
  */
public class RedisProperties {
	static final String PROPERTIES_FILE_NAME = "redis.properties";
	static PropertiesHelper props;
	public static String applicationPath;

	@SuppressWarnings("rawtypes")
	private static void loadProperties() {
		try {
			if (props == null) {
				System.out.println("Load [redis.properties] from classpath");
				Properties properties = new Properties();
				Enumeration urls = RedisProperties.class.getClassLoader().getResources(PROPERTIES_FILE_NAME);
				while (urls.hasMoreElements()) {
					URL url = (URL) urls.nextElement();
					InputStream input = null;
					try {
						URLConnection con = url.openConnection();
						con.setUseCaches(false);
						input = con.getInputStream();
						properties.load(input);
					} finally {
						if (input != null) {
							input.close();
						}
					}
				}
				props = new PropertiesHelper(properties);
				for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
					Map.Entry entry = (Map.Entry) it.next();
					System.out.println("[Property] " + entry.getKey() + "=" + entry.getValue());
				}
				System.out.println();
			}
		} catch (IOException e) {
			throw new RuntimeException("在classpath中未找到redis.properties配置文件!", e);
		}
	}

	public static Properties getProperties() {
		return getHelper().getProperties();
	}

	private static PropertiesHelper getHelper() {
		if (props == null)
			loadProperties();
		return props;
	}

	public static String getProperty(String key, String defaultValue) {
		return getHelper().getProperty(key, defaultValue);
	}

	public static String getProperty(String key) {
		return getHelper().getProperty(key);
	}

	public static String getRequiredProperty(String key) {
		return getHelper().getRequiredProperty(key);
	}

	public static int getRequiredInt(String key) {
		return getHelper().getRequiredInt(key);
	}

	public static int getInt(String key, int defaultValue) {
		return getHelper().getInt(key, defaultValue);
	}

	public static boolean getRequiredBoolean(String key) {
		return getHelper().getRequiredBoolean(key);
	}

	public static String getNullIfBlank(String key) {
		return getHelper().getNullIfBlank(key);
	}

	public static void setProperty(String key, String value) {
		getHelper().setProperty(key, value);
	}

	public static void setProperties(Properties v) {
		props = new PropertiesHelper(v);
	}

	public static String getRealPath(String reportTemplateDir, String string) {
		String path = getProperty(reportTemplateDir, string);
		if (!StringUtils.isEmpty(applicationPath)) {
			path = applicationPath + path;
		}
		return path;
	}
}

配置文件处理工具类:

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.sess.june17th.common.base.SystemProperties;
/**
 * 配置文件支持类
 * @author liux
 * @Date 2017年8月12日 下午5:51:29
 */
public class PropertiesHelper {

	Properties p;

	public PropertiesHelper(Properties p) {
		this.p = p;
	}

	public Properties getProperties() {
		return this.p;
	}

	public String getProperty(String key, String defaultValue) {
		return getProperties().getProperty(key, defaultValue);
	}

	public String getProperty(String key) {
		return getProperties().getProperty(key);
	}

	public String getRequiredProperty(String key) {
		String value = getProperty(key);
		if ((value == null) || ("".equals(value.trim()))) {
			throw new IllegalStateException("required property is blank by key=" + key);
		}
		return value;
	}

	public Integer getInt(String key) {
		if (getProperty(key) == null) {
			return null;
		}
		return Integer.valueOf(Integer.parseInt(getRequiredProperty(key)));
	}

	public int getInt(String key, int defaultValue) {
		if (getProperty(key) == null) {
			return defaultValue;
		}
		return Integer.parseInt(getRequiredProperty(key));
	}

	public int getRequiredInt(String key) {
		return Integer.parseInt(getRequiredProperty(key));
	}

	public Boolean getBoolean(String key) {
		if (getProperty(key) == null) {
			return null;
		}
		return Boolean.valueOf(Boolean.parseBoolean(getRequiredProperty(key)));
	}

	public boolean getBoolean(String key, boolean defaultValue) {
		if (getProperty(key) == null) {
			return defaultValue;
		}
		return Boolean.parseBoolean(getRequiredProperty(key));
	}

	public boolean getRequiredBoolean(String key) {
		return Boolean.parseBoolean(getRequiredProperty(key));
	}

	public String getNullIfBlank(String key) {
		String value = getProperties().getProperty(key);
		if ((value == null) || ("".equals(value.trim()))) {
			return null;
		}
		return value;
	}

	public PropertiesHelper setProperty(String key, String value) {
		this.p.setProperty(key, value);
		return this;
	}

	public void clear() {
		this.p.clear();
	}

	public Set<Map.Entry<Object, Object>> entrySet() {
		return this.p.entrySet();
	}

	public Enumeration<?> propertyNames() {
		return this.p.propertyNames();
	}

	@SuppressWarnings("rawtypes")
	public static Properties loadAllPropertiesFromClassLoader(String resourceName) throws IOException {
		Properties properties = new Properties();
		Enumeration urls = SystemProperties.class.getClassLoader().getResources(resourceName);
		while (urls.hasMoreElements()) {
			URL url = (URL) urls.nextElement();
			InputStream input = null;
			try {
				URLConnection con = url.openConnection();
				con.setUseCaches(false);
				input = con.getInputStream();
				properties.load(input);
			} finally {
				if (input != null) {
					input.close();
				}
			}
		}
		return properties;
	}

}

redisutil工具类:


import com.sess.june17th.common.config.RedisProperties;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;

import java.io.*;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Redis 工具类
 */

public class RedisUtil {

//	protected static ReentrantLock lockPool = new ReentrantLock();
	protected static final ReentrantLock lockJedis = new ReentrantLock();

	private static Logger _log = LoggerFactory.getLogger(RedisUtil.class);

	// Redis服务器IP
	private static String IP = RedisProperties.getProperty("redis.ip","127.0.0.1");

	// Redis的端口号
	private static int PORT = Integer.valueOf(RedisProperties.getProperty("redis.port","3306"));

	// 访问密码
	private static String PASSWORD = AESUtil.AESDecode(RedisProperties.getProperty("redis.password",null));
	// 可用连接实例的最大数目,默认值为8;
	// 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
	private static int MAX_ACTIVE = Integer.valueOf(RedisProperties.getProperty("redis.max_active","500"));

	// 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
	private static int MAX_IDLE = Integer.valueOf(RedisProperties.getProperty("redis.max_idle","5"));

	// 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
	private static int MAX_WAIT = Integer.valueOf(RedisProperties.getProperty("redis.max_wait","10000"));

	// 超时时间
	private static int TIMEOUT = Integer.valueOf(RedisProperties.getProperty("redis.timeout","10000"));
	// redis数据库db选择(默认db0)
	private static int JEDIS_DB_INDEX = Integer.valueOf(RedisProperties.getProperty("redis.db_index","0"));

	// 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
	private static boolean TEST_ON_BORROW = false;

	private static volatile  JedisPool jedisPool = null;

	/**
	 * redis过期时间,以秒为单位
	 */
	public final static int EXRP_HOUR = 60 * 60; // 一小时
	public final static int EXRP_DAY = 60 * 60 * 24; // 一天
	public final static int EXRP_MONTH = 60 * 60 * 24 * 30; // 一个月

	static {
		try {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(MAX_ACTIVE);
			config.setMaxIdle(MAX_IDLE);
			config.setMaxWaitMillis(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
			jedisPool = new JedisPool(config, IP, PORT, TIMEOUT);
		} catch (Exception e) {
			_log.error("First create JedisPool error : " + e);
		}
	}

	/**
	 * 初始化Redis连接池
	 */
	private static void initialPool() {
		if (jedisPool != null) {
			try {
				JedisPoolConfig config = new JedisPoolConfig();
				config.setMaxTotal(MAX_ACTIVE);
				config.setMaxIdle(MAX_IDLE);
				config.setMaxWaitMillis(MAX_WAIT);
				config.setTestOnBorrow(TEST_ON_BORROW);
				jedisPool = new JedisPool(config, IP, PORT, TIMEOUT);
			} catch (Exception e) {
				_log.error("First create JedisPool error : " + e);
			}
		}
	}

	/**
	 * 在多线程环境同步初始化
	 */
	private static synchronized void poolInit() {
		if (null == jedisPool) {
			initialPool();
		}
	}

	/**
	 * 同步获取Jedis实例
	 * 
	 * @return Jedis
	 */
	public synchronized static Jedis getJedis() {
		poolInit();
		Jedis jedis = null;
		try {
			if (null != jedisPool) {
				jedis = jedisPool.getResource();
				try {
					jedis.auth(PASSWORD);
				} catch (Exception e) {

				}
			}
		} catch (Exception e) {
			_log.error("Get jedis error : " + e);
			_log.error(" ip-> " + IP + " port-> " + PORT);
		}
		return jedis;
	}

	/**
	 * 设置 String
	 * 
	 * @param key
	 * @param value
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public synchronized static void set(String key, String value,Integer dbSelect) {
		Jedis jedis = null;
		try {
			value = StringUtils.isBlank(value) ? "" : value;
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.set(key, value);
			jedis.expire(key, EXRP_MONTH);
		} catch (Exception e) {
			_log.error("Set key error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 设置 byte[]
	 * 
	 * @param key
	 * @param value
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public synchronized static void set(byte[] key, byte[] value,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.set(key, value);
			jedis.expire(key, EXRP_MONTH);
		} catch (Exception e) {
			_log.error("Set key error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 设置 String 过期时间
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @param dbSelect (设置DB库,传null默认为db0)
	 *            以秒为单位
	 */
	public synchronized static void set(String key, String value, int seconds,Integer dbSelect) {
		Jedis jedis = null;
		try {
			value = StringUtils.isBlank(value) ? "" : value;
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.setex(key, seconds, value);
		} catch (Exception e) {
			_log.error("Set keyex error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 设置 byte[] 过期时间
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @param dbSelect (设置DB库,传null默认为db0)
	 *            以秒为单位
	 */
	public synchronized static void set(byte[] key, byte[] value, int seconds,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.set(key, value);
			jedis.expire(key, seconds);
		} catch (Exception e) {
			_log.error("Set key error : " + e);
		} finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 获取String值
	 * 
	 * @param key
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @return value
	 */
	public synchronized static String get(String key,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			if (null == jedis) {
				return null;
			}
			String value = jedis.get(key);
			return value;
		}catch(Exception e){

		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 获取byte[]值
	 * 
	 * @param key
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @return value
	 */
	public synchronized static byte[] get(byte[] key,Integer dbSelect) {
		Jedis jedis = null;
		try {

			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			if (null == jedis) {
				return null;
			}
			byte[] value = jedis.get(key);
			return value;
		}catch(Exception e){

		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
		return null;

	}

	/**
	 * 删除值
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @param key
	 */
	public synchronized static void remove(String key,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.del(key);
		} catch (Exception e) {
			_log.error("Remove keyex error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 删除值
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @param key
	 */
	public synchronized static void remove(byte[] key,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.del(key);
		} catch (Exception e) {
			_log.error("Remove keyex error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * lpush 将数据放入队列中
	 * 
	 * @param key
	 *            队列名称
	 *            @param dbSelect (设置DB库,传null默认为db0)
	 * @param values
	 *            数据
	 */
	public synchronized static void lpush(String key,Integer dbSelect, String... values) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.lpush(key, values);
		} catch (Exception e) {
			_log.error("lpush error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * lrem
	 * 
	 * @param key
	 * @param count
	 * @param value
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public synchronized static void lrem(String key, long count, String value,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.lrem(key, count, value);
		} catch (Exception e) {
			_log.error("lpush error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * sadd
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public synchronized static void sadd(String key, String value, int seconds,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.sadd(key, value);
			jedis.expire(key, seconds);
		} catch (Exception e) {
			_log.error("sadd error : " + e);
		} finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 将本次处理的任务从srckey队列缓存到dstkey队列中
	 * 
	 * @param srckey
	 *            缓存队列
	 * @param dstkey
	 *            目标队列
	 *            @param dbSelect (设置DB库,传null默认为db0)
	 */
	public synchronized static String rpoplpush(String srckey, String dstkey,Integer dbSelect) {
		String result = null;
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			// 将本次处理失败的任务从暂存队列"tmp-queue"中,弹回任务队列"task-queue"
			result = jedis.rpoplpush(srckey, dstkey);
		} catch (Exception e) {
			_log.error("sadd error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 将本次任务从暂存队列中清除
	 * 
	 * @param key
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public synchronized static void rpop(String key,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			// 将本次处理失败的任务从暂存队列"tmp-queue"中,弹回任务队列"task-queue"
			jedis.rpop(key);
		} catch (Exception e) {
			_log.error("sadd error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 功能简述: 对实体Bean进行序列化操作.
	 * 
	 * @param source
	 *            待转换的实体
	 * @return 转换之后的字节数组
	 * @throws Exception
	 */
	private static byte[] serialize(final Object source) {
		ByteArrayOutputStream byteOut = null;
		ObjectOutputStream ObjOut = null;
		try {
			byteOut = new ByteArrayOutputStream();
			ObjOut = new ObjectOutputStream(byteOut);
			ObjOut.writeObject(source);
			ObjOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
			_log.error(source.getClass().getName() + " serialized error !", e);
		} finally {
			try {
				if (null != ObjOut) {
					ObjOut.close();
				}
			} catch (IOException e) {
				ObjOut = null;
			}
		}
		return byteOut.toByteArray();
	}

	/**
	 * 功能简述: 将字节数组反序列化为实体Bean.
	 * 
	 * @param source
	 *            需要进行反序列化的字节数组
	 * @return 反序列化后的实体Bean
	 * @throws Exception
	 */
	private static Object deserialize(final byte[] source) {
		ObjectInputStream ObjIn = null;
		Object retVal = null;
		try {
			ByteArrayInputStream byteIn = new ByteArrayInputStream(source);
			ObjIn = new ObjectInputStream(byteIn);
			retVal = ObjIn.readObject();
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("deserialized error  !", e);
		} finally {
			try {
				if (null != ObjIn) {
					ObjIn.close();
				}
			} catch (IOException e) {
				ObjIn = null;
			}
		}
		return retVal;
	}

	/**
	 * 从缓存中获取对象,如果类实现了序列化接口则使用Java反序列化解码,否则用JSON反解码
	 * 
	 * @param key
	 * @param clazz
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getFromCache(final String key, final Class<T> clazz,Integer dbSelect) {
		if (Serializable.class.isAssignableFrom(clazz)) {
			return (T) getFromCacheWithDeserialize(key,dbSelect);
		} else {
			String json = getFromRedis(key,dbSelect);
			if (json != null) {
				return JSONUtil.fromJson(json, clazz);
			}
		}
		return null;
	}

	/**
	 * 获取缓存并反序列化成对象
	 * 
	 * @param key
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @return
	 */
	public static Object getFromCacheWithDeserialize(String key,Integer dbSelect) {
		try {
			byte[] data = getBytesFromRedis(key,dbSelect);
			if (data != null)
				return deserialize(data);
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("获取缓存并反序列化异常", e);
		}
		return null;
	}

	/**
	  * @Description: TODO
	  * @param ${tags}
	 @param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:53
	  */
	private static byte[] getBytesFromRedis(final String key,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			byte[] bytes = jedis.get(key.getBytes());
			return bytes;
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * 序列化对象并存入缓存
	 * 
	 * @param key
	 * @param obj
	 * @param expireSeconds
	 * @param dbSelect (设置DB库,传null默认为db0)
	 *            过期时间,秒
	 */
	public static void putIntoCacheWithSerialize(final String key, final Object obj, final int expireSeconds,Integer dbSelect) {
		try {

			/*
			 * if(obj instanceof Serializable){ byte[] data = serialize(obj);
			 * if(data!=null){ putIntoRedis(key, data, expireSeconds); } }else{
			 */
			putIntoCacheWithJson(key, obj, expireSeconds, dbSelect);
			// }

		} catch (Exception e) {
			e.printStackTrace();
			_log.error("序列化对象并存缓存异常", e);
		}
	}

	/**
	  * @Description: TODO
	  * @@param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:54
	  */
	// 序列化方式存 针对微信unitid添加的方法,旧工程用的序列化方式存储
	public static void putIntoCacheToSerialize(final String key, final Object obj,Integer dbSelect) {
		int time = 5 * 365 * 24 * 60 * 60;
		if (obj instanceof Serializable) {
			byte[] data = serialize(obj);
			if (data != null) {
				putIntoRedis(key, data, time);
			}
		} else {
			putIntoCacheWithJson(key, obj, time, dbSelect);
		}
	}

	/**
	  * @Description: TODO
	  * @param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:54
	  */
	public static void putIntoCacheToSerialize(final String key, final Object obj, final int expireSeconds,Integer dbSelect) {
		if (obj instanceof Serializable) {
			byte[] data = serialize(obj);
			if (data != null) {
				putIntoRedis(key, data, expireSeconds);
			}
		} else {
			putIntoCacheWithJson(key, obj, expireSeconds, dbSelect);
		}

	}

	/**
	 * 序列化对象并存入缓存,永久存储(将造成缓存脏数据,不建议使用)
	 * 
	 * @param key
	 * @param obj
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public static void putIntoCacheWithSerialize(final String key, final Object obj,Integer dbSelect) {
		putIntoCacheWithSerialize(key, obj, 24 * 60 * 60, dbSelect);
	}

	private static void putIntoRedis(final String key, final byte[] data, final int expireSeconds) {
		Jedis jedis = RedisUtil.getJedis();
		try {
			if (expireSeconds < 1)
				jedis.set(key.getBytes(), data);
			else
				jedis.setex(key.getBytes(), expireSeconds, data);
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * 将对象转成JSON并存入缓存
	 * 
	 * @param key
	 * @param obj
	 * @param expireSeconds
	 * @param dbSelect (设置DB库,传null默认为db0)
	 *            过期时间,秒
	 */
	public static void putIntoCacheWithJson(final String key, final Object obj, final int expireSeconds,Integer dbSelect) {
		try {
			String json = JSONUtil.toJson(obj);
			if (json != null) {
				putStringIntoCache(key, json, expireSeconds, dbSelect);
			}
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("将对象转成JSON并存入缓存异常", e);
		}
	}

	/**
	 * 将字符串存入缓存
	 * 
	 * @param key
	 * @param value
	 * @param expireSeconds
	 * @param dbSelect (设置DB库,传null默认为db0)
	 *            过期时间,秒
	 */
	public static void putStringIntoCache(final String key, final String value, final int expireSeconds,Integer dbSelect) {
		try {
			putIntoRedis(key, value, expireSeconds, dbSelect);
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("缓存字符串异常", e);
		}
	}

	/**
	  * @Description: TODO
	  * @param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:54
	  */
	private static void putIntoRedis(final String key, final String data, final int expireSeconds,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			if (expireSeconds < 1)
				jedis.set(key, data);
			else
				jedis.setex(key, expireSeconds, data);
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	  * @Description: TODO
	  *@param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:54
	  */
	public static <T> T getFromCacheWithJson(String key, String field, Class<T> clazz,Integer dbSelect) {
		try {
			String json = getFromRedis(key, field,dbSelect);
			if (json != null) {
				return JSONUtil.fromJson(json, clazz);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	  * @Description: TODO
	  * @param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:55
	  */
	private static String getFromRedis(String key, String field,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			return jedis.hget(key, field);
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	  * @Description: TODO
	  *@param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:55
	  */
	public static void hsetWithJson(final String key, final String field, final Object obj, final int expireSeconds,Integer dbSelect) {
		try {
			String json = JSONUtil.toJson(obj);
			if (json != null) {
				putIntoRedis(key, field, json, expireSeconds,dbSelect);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	  * @Description: TODO
	  * @param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:55
	  */
	private static void putIntoRedis(final String key, final String field, final String data, final int expireSeconds,Integer dbSelect) {

		Jedis jedis = null;
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.hset(key, field, data);
			if (expireSeconds > 0) {
				jedis.expire(key, expireSeconds);
			}
		} finally {
			if (jedis != null)
				jedis.close();
		}

	}

	/**
	 * 获取缓存并反JSON成对象
	 * 
	 * @param key
	 * @param clazz
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @return
	 */
	public static <T> T getFromCacheWithJson(final String key, final Class<T> clazz,Integer dbSelect) {
		try {
			String json = getFromRedis(key, dbSelect);
			if (json != null) {
				return JSONUtil.fromJson(json, clazz);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	  * @Description: TODO
	  * @param ${tags}
	 @param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:55
	  */
	public static String getFromRedis(final String key,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			return jedis.get(key);
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	  * @Description: TODO
	  *@param dbSelect (设置DB库,传null默认为db0)
	  * @return ${return_type}
	  * @throws
	  * @author liuxue
	  * @date 2019/7/29 0029 下午 3:55
	  */
	public static void setIncr(final String key, final int expireSeconds,Integer dbSelect) {
		Jedis jedis = null;
		try {
			RedisUtil.lockJedis.lock();
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			String str = jedis.get(key); // 取一下
			long count = jedis.incr(key);
			if (count == 1 && (null == str || "".equals(str))) { // 取不到并且插入成功了 也就是第一次的情况持久化
				jedis.expire(key, expireSeconds);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisUtil.lockJedis.unlock();
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * 通过redis的incr特性保证key值的唯一
	 * 
	 * @param key
	 * @param expireSeconds
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @return
	 */
	public static boolean setIncrKey(final String key, final int expireSeconds,Integer dbSelect) {
		// Jedis jedis = new Jedis("192.168.18.188", 6380); //redis服务器的ip和端口
		Jedis jedis = null;
		try {
			RedisUtil.lockJedis.lock();
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			long count = jedis.incrBy(key, 1);
			if (count == 1) {
				jedis.expire(key, expireSeconds);
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisUtil.lockJedis.unlock();
			if (jedis != null)
				jedis.close();
		}
		return false;
	}

	/**
	 * 根据key删除redis缓存
	 * 
	 * @param key
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public static void deleteFromRedis(String key,Integer dbSelect) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			jedis.del(key);
		} catch (Exception e) {
			_log.error("删除缓存异常|" + key + "|" + e);
			e.printStackTrace();
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * 批量删除以 preStr 字符开头的缓存数据
	 * 
	 * @param preStr
	 * @param dbSelect (设置DB库,传null默认为db0)
	 */
	public static void batchDel(String preStr,Integer dbSelect) {
		Jedis jedis = null;
		// Jedis jedis = new Jedis("192.168.18.188", 6380); //redis服务器的ip和端口
		try {
			jedis = RedisUtil.getJedis();
			jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
			Set<String> set = jedis.keys(preStr + "*");
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				String keyStr = it.next();
				jedis.del(keyStr);
			}
		} catch (Exception e) {
			_log.error("删除缓存异常|" + preStr + "|" + e);
			e.printStackTrace();
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * 批量获取缓存中的数据
	 * 
	 * @param preStr
	 * @param dbSelect (设置DB库,传null默认为db0)
	 * @return
	 */
	public Set<String> getBatchRedis(String preStr,Integer dbSelect) {
		Jedis jedis = RedisUtil.getJedis();
		jedis.select(dbSelect == null?JEDIS_DB_INDEX:dbSelect);
		return jedis.keys(preStr + "*");
	}

	/**
	 * 获取锁
	 * 
	 * @param lockName
	 *            锁名称 ,默认传redis缓存的key
	 * @param waitTimeOut
	 *            等待获取锁的超时时间(秒)
	 * @param lockTimeOut
	 *            锁的生存时间(秒)
	 * @return 如果获取锁成功则返回锁键对应值,否则返回null
	 */
	public static String addLockWithTimeOut(String lockName, int waitTimeOut, int lockTimeOut) {
		Jedis conn = RedisUtil.getJedis();
		try {
			String lockKey = "lock:" + lockName;
			String lockId = UUID.randomUUID().toString();
			int now = 0;
			int end = now + waitTimeOut;
			while (now < end) {
				if (conn.setnx(lockKey, lockId).intValue() == 1) {
					conn.expire(lockKey, lockTimeOut);
					_log.info("获取锁成功|acquirelock|'" + lockName + "'|锁标识|" + lockId + "|获取锁次数| " + (now + 1));
					return lockId;
				}
				if (conn.ttl(lockKey) < 0) {
					conn.expire(lockKey, lockTimeOut);
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					Thread.currentThread().interrupt();
				}
				_log.info("获取锁等待|acquirelock|'" + lockName + "'|锁标识|" + lockId + "|获取锁重试次数| " + (now + 1));
				now++;
			}
		} catch (Exception e) {
			_log.error("获取锁|redis加锁异常|" + lockName + "|" + e);
			e.printStackTrace();
		} finally {
			if (conn != null)
				conn.close();
		}
		return null;
	}

	/**
	 * 释放锁 解锁时将判断锁键对应值是否是给定的值,防止误解锁。
	 * 
	 * @param lockName
	 *            锁名称
	 * @param lockId
	 *            锁键对应值
	 * @param waiteTimeOut
	 *            解锁动作的超时时间(秒)
	 * @return true如果解锁成功,否则返回false
	 */
	public static boolean releaseLock(String lockName, String lockId, int waiteTimeOut) {
		Jedis conn = RedisUtil.getJedis();
		try {
			String lockKey = "lock:" + lockName;
			long now = 0;
			long end = now + waiteTimeOut;
			while (now < end) {
				conn.watch(lockKey); // 监视锁的键
				if (null != conn.get(lockKey) && lockId.equals(conn.get(lockKey))) { // 判断锁的值是否和加锁时设置的一致,即检查进程是否仍然持有锁
					Transaction trans = conn.multi();
					trans.del(lockKey); // 在Redis事务中释放锁
					List<Object> exec = trans.exec();
					if (exec != null) { // 事务执行失败后重试(监视的键被修改导致事务失败,重新监视并释放锁)
						_log.info("释放锁成功|releaseLock|'" + lockName + "'|锁标识|" + lockId + "|释放锁次数| " + (now + 1));
						return true;
					}
					now++;
					continue;
				} else {
					_log.info("锁已释放|releaseLock|'" + lockName + "'|锁标识|" + lockId + "|过期自动释放");
				}
				conn.unwatch(); // 解除监视
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("释放锁|redis解锁异常|" + lockName + "|" + e);
			e.printStackTrace();
		} finally {
			if (conn != null)
				conn.close();
		}
		return false;
	}

	/**
	 * @Title: hgetWithJson
	 * @Description: TODO
	 * @return: T
	 */
	public static <T> T hgetWithJson(final String key, String field, Class<T> clazz) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			String ret = jedis.hget(key, field);
			if (StringUtils.isNotBlank(ret)) {
				return JSONUtil.fromJson(ret, clazz);
			}
			return null;
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * @Description: 删除缓存哈希结构中的一条
	 * @Param: [key, field]
	 * @return: void
	 */
	public synchronized static void hdel(final String key, String field) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.hdel(key, field);
		} catch (Exception e) {
			_log.error("Remove keyex error : " + e);
		}finally{
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 获取redis set list
	 * 
	 * @function:
	 * @since 1.0
	 * @param key
	 * @param clazz
	 * @return
	 */
	public static <T> Map<String, T> hgetAllWithJson(final String key, Class<T> clazz) {
		Jedis jedis = null;
		try {
			jedis = getJedis();

			Map<String, String> retmap = jedis.hgetAll(key);
			if (retmap != null) {
				Map<String, T> map = new HashMap<>();
				for (Map.Entry<String, String> entry : retmap.entrySet()) {
					if (StringUtils.isNotBlank(entry.getValue())) {
						T t = JSONUtil.fromJson(entry.getValue(), clazz);
						map.put(entry.getKey(), t);
					}
				}
				return map;
			}
			return null;
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * 
	 * @Description: 返回List
	 * @param key
	 * @param clazz
	 * @return List<T>
	 */
	public static <T> List<T> getListFromCacheWithJson(String key, Class<T> clazz, JsonConfig jsonConfig,Integer dbSelect) {
		try {
			String json = getFromRedis(key,dbSelect);
			if (json != null) {
				return JSONUtil.fromListJson(json, clazz, jsonConfig);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public static void putListIntoCacheWithSerialize(String key, List list, int expire, JsonConfig jsonConfig,Integer dbSelect) {
		JSONArray jsonArray = null;
		if (null != jsonConfig) {
			jsonArray = JSONArray.fromObject(list, jsonConfig);
		} else {
			jsonArray = JSONArray.fromObject(list);
		}
		if (null == jsonArray) {
			_log.info("存入缓存异常");
			return;
		}
		String json = jsonArray.toString();
		if (json != null) {
			try {
				putStringIntoCache(key, json, expire, dbSelect);
			} catch (Exception e) {
				_log.info("存入缓存异常");
				e.printStackTrace();
				return;
			}
		}
	}

	public static void updateSigninVerifyIssueTime(String key, String value, int timeOut) {
		Jedis jedis = null;
		try {
			value = StringUtils.isBlank(value) ? "" : value;
			jedis = getJedis();
			jedis.set(key, value);
			jedis.expire(key, timeOut);
		} catch (Exception e) {
			_log.error("Set key error : " + e);
		}finally {
			if(null != jedis){
				jedis.close();
			}
		}
	}

	/**
	 * 返回集合中的所有成员
	 * 
	 * @param key
	 * @return Set<String>
	 */
	public static Set<String> smembers(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.smembers(key);
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("返回集合中的所有成员出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 向集合添加一个成员,返回添加成功的数量
	 * 
	 * @param key
	 * @param members
	 * @return Long
	 */
	public static Long sadd(String key, String members) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.sadd(key, members);
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("向集合添加一个成员出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 获取集合的成员数量
	 * 
	 * @param key
	 * @return
	 */
	public static Long scard(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.scard(key);
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("获取集合的成员数出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 获取集合中指定数量的元素
	 * 
	 * @param key
	 * @return
	 */
	public static List<String> srandmember(String key, int count) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.srandmember(key, count);
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("获取集合中指定数量的元素出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 移除集合中的一个成员元素
	 * 
	 * @param key
	 * @return 1表示成功,0表示移除元素不是集合中的元素,-1表示出错
	 */
	public static Long srem(String key, String members) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.srem(key, members);
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("移除集合中的一个成员元素出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return -1L;
	}

	/**
	 * 将一个值插入到列表头部,value可以重复,返回列表的长度
	 * 
	 * @param key
	 * @param value
	 *            String
	 * @return 返回List的长度 null表示出错
	 */
	public static Long lpush(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			Long length = jedis.lpush(key, value);
			return length;
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("将一个值插入到列表头部出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 获取列表长度,key为空时返回0
	 * 
	 * @param key
	 * @return Long null表示出错
	 */
	public static Long llen(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			Long length = jedis.llen(key);
			return length;
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("获取列表长度出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 在列表中的尾部添加一个值,返回列表的长度
	 * 
	 * @param key
	 * @param values
	 * @return Long null表示出错
	 */
	public static Long rpush(String key, String values) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			Long length = jedis.rpush(key, values);
			return length;
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("在列表中的尾部添加一个值出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 移出并获取列表的第一个元素,当列表不存在或者为空时,返回Null
	 * 
	 * @param key
	 * @return String 当列表不存在或者为空时,返回Null
	 */
	public static String lpop(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			String value = jedis.lpop(key);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("移出并获取列表的第一个元素出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}

	/**
	 * 
	 * 设置 key的有效时间(按秒计算)
	 * 
	 * @param key
	 *            key
	 * @param expireSeconds
	 *            秒
	 * @return 1表示成功,0表示key不存在,null表示出错
	 */
	public static Long expire(final String key, final int expireSeconds) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			Long value = jedis.expire(key, expireSeconds);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			_log.error("设置 key的有效时间(按秒计算)出错:", e);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return null;
	}
	
	/**
	 * 
	* @Description: 
	* @return    
	* String  
	 */
	public static synchronized long getCount() {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			Long rs = jedis.incr("transfer_file_count");
			return rs;
		} catch (Exception e) {
			
		}finally {
			if( null != jedis) {
				jedis.close();
			}
		}
		return 0;
	}
	
	/**
	 * 通过redis的incr特性保证key值的唯一
	 * @param key 
	 * @param expireSeconds 过期时间,秒;仅第一次设置key时设置
	 * @return 返回增长次数,第一次是1,此后key未过期则会累计,出错返回0
	 */
	public static Long setIncrReturn(final String key, final int expireSeconds) {
		Jedis jedis = null;
		try {
			RedisUtil.lockJedis.lock();
			jedis = RedisUtil.getJedis();
			String str = jedis.get(key); // 取一下
			long count = jedis.incr(key);
			if (count == 1 && (null == str || "".equals(str))) { // 取不到并且插入成功了 也就是第一次的情况持久化
				jedis.expire(key, expireSeconds);
			}
			return count;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisUtil.lockJedis.unlock();
			if (jedis != null)
				jedis.close();
		}
		return 0L;
	}

	/**
	 * 
	 * @param key 关键字
	 * @param expireSeconds 第一次设置时key的过期时间,秒
	 * @return 返回原来value加1后的值,若原来key不存在,则value初始化为0然后加1返回1
	 */
	public static long setAndgetIncr(final String key, final int expireSeconds) {
		Jedis jedis = null;
		try {
			RedisUtil.lockJedis.lock();
			jedis = RedisUtil.getJedis();
			String str = jedis.get(key); // 取一下
			long count = jedis.incr(key);
			if (count == 1 && (null == str || "".equals(str))) { // 取不到并且插入成功了 也就是第一次的情况持久化
				jedis.expire(key, expireSeconds);
			}
			return count;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			RedisUtil.lockJedis.unlock();
			if (jedis != null)
				jedis.close();
		}
		return 0L;
	}
}

以上就集成完了 可以正常使用了,工具类的方法非常全面,支持设置db;

详解spring boot starter redis配置文件 spring-boot-starter-Redis主要是通过配置RedisConnectionFactory中的相关参数去实现连接redis service。下面通过本文给大家介绍在spring boot的配置文件中redis的基本配置,需要的的朋友参考下 立即下载

相关推荐

硕士论文排版救星:用Mathtype一键搞定所有公式格式(附样式预设文件)

本文详细介绍了如何使用Mathtype的样式预设和批量格式化功能,快速统一硕士论文中的公式格式。通过创建标准样式预设文件和一键批量处理,解决公式字体、大小、间距不统一的问题,确保符合学术规范要求,大幅提升论文排版效率。

weixin_30772105的博客 943

springboot-redis读取配置文件

1,配置文件 redis: clusterNodes: - 10.108.6.90:6179 - 10.108.6.90:6279 - 10.108.6.90:6379 - 10.108.6.90:6479 - 10.108.6.90:6579 ...

C18298182575的博客 4026

TiDB实战篇-数据库写入慢常见处理方式

TiDB实战篇-数据库写入慢常见处理方式

S1124654的博客 1011

redis config配置

https://www.cnblogs.com/ysocean/p/9074787.html

wenxuanyue的博客 3954

Python之ae-toolbox包语法、参数和实际应用案例

ae-toolbox(全称 after-effects-toolbox)是 Python 生态中专门用于 Adobe After Effects(AE)自动化操作 的工具包,基于 AE 的 ExtendScript(JSX)桥接技术,实现 Python 与 AE 的跨语言通信。

王国平的博客 1174

14、Java 中 TAF 和 FCF 的使用及 Oracle 数据库升级与迁移指南

本文详细介绍了Java中实现高可用性的TAF(透明应用故障转移)和FCF(快速连接故障转移)技术,以及如何将Oracle 9iR2 RAC环境升级到10gR2 RAC,并将数据从OCFS迁移到ASM。通过示例代码和操作步骤,帮助开发者和数据库管理员更好地应对数据库故障和数据迁移问题,提升系统的可用性和性能。

egg99的博客 111

Redis简洁使用

一、pom文件添加redis依赖&lt;!--redis--&gt; &lt;dependency&gt; &lt;groupId&gt;redis.clients&lt;/groupId&gt; &lt;artifactId&gt;jedis&lt;/artifactId&gt; &lt;version&gt;2.6.1&lt;/version&gt; &lt

sln 263

运营商拼命封杀的PCDN到底是个啥?

最近一段时间,很多宽带用户发现自家的网速莫名其妙变慢了,甚至收到了运营商发来的“违规使用宽带”警告短信,严重的甚至直接被断网。很多人一头雾水:我交了钱办的宽带,自己买的插件或者京东云、网心云这些硬件赚点“电费”,怎么就成了运营商眼中的“过街老鼠”?今天,我们就把这层窗户纸捅破,深度剖析这个让互联网大厂省下几十亿、让运营商恨得牙痒痒、让用户夹在中间进退两难的技术黑话——。

网络技术联盟站 924

SpringBoot中操作redis及相关配置文件

springboot操作redis的相关设置和例子

sxycylq的博客 900

智能车图像处理14-进阶篇6--坡道的判定

这篇文章主要讲述智能车图像处理中 : 如何判断坡道

taiyuezyh的博客 9236

实现画布拖拽算子排布,通过flink实现算子编排计算,如何实现?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值 。

**My Coding Family** 1072

快速入门 - Spring Boot 集成 Redis 配置及使用

Spring Boot 集成 Redis 配置及使用 (代码+详细注释)

CYK_byte的博客 1万+

【异常】Milvus 向量数据库字段缺失错误分析与解决 MilvusVectorStoreServiceV2 - Failed to insert batch [0-1]: The field:

本文分析了Milvus向量数据库插入操作中出现的字段缺失错误。错误显示age_mode字段未提供,导致批量插入失败。根本原因是Milvus Schema要求所有定义字段必须存在,但代码中对部分字段采用条件添加方式。解决方案是将条件添加改为始终添加,为可能为null的字段提供空字符串默认值。文章总结了Schema约束、防御性编程等经验,并建议通过Schema变更检查、单元测试覆盖和数据校验层等预防措施避免类似问题。该问题影响product、mac、source等多个字段,修改后确保了数据插入的可靠性。

奶爸的编程之路,也就一周冷个三天 412

七天玩转Redis | Day6、SpringBoot集成Redis

号外号外????????????:小猿和优快云联合推出的“七天玩转Redis,打卡还能领周边”活动开始啦!点击查看????????【报名及活动细则】????????,轻轻松松和小伙伴们一起拿奖励吧! PS:零基础也能入门Redis哟,快去一起参与拿周边吧! 博主简介????????‍⚕️:国内某一线互联网公司Java工程师????????‍????,业余自媒体创作者????,优快云博客专家????,Java领域优质创作者????,华为云享专家????,华为HDZ核心成员????‍????,曾发表并

累计发表博客60余万字,优快云 博客专家、Java 领域优质创作者、华为云享专家、阿里云专家博主。专注分享 Java 优质文章,毕设实战项目开发,全网粉丝10W+ 2003

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Redis客户端使用(Client、Java、SpringBoot)

由于可能有多个客户端想要跨主机访问Linux服务器的其他服务器的端口号,ssh为了区分这些端口号,就会把Linux的各种服务的端口号映射到本地主机(发出访问请求的主机)的其他端口号(比如Linux的Redis端口6379映射到本地主机8888端口),从而达到客户端访问本地端口即可实现访问Redis端口,这就是ssh的端口转发功能。其余的命令不再演示,Jedis提供的Java客户端访问Redis的命令基本和Redis客户端的命令一致,使用很简单,只是需要注意返回值和变长参数(或者是Map来组织变长参数)。

sniper_fandc的博客 1318

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

七天玩转Redis | 第6天 SpringBoot集成Redis

今天这一节我们主要来讲一下Redis在实际项目开发中的主要使用,一般Redis会结合spring系列框架使用,最常见的就是SpringBoot集成Redis。 一、SpringBoot集成Redis: 先创建一个springboot项目以及配置好maven 然后在pom.xml里面导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-bo

lovestar墨竹的博客 908

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

redis哨兵模式的配置及在springboot中使用

redis哨兵模式的配置及在springboot中使用 一、从0搭建redis哨兵模式 1、下载redis http://download.redis.io/releases/ 这里使用的6.0.4版本 下载后上传到服务器中 这里准备三台服务器:192.168.200.135(主) 192.168.200.136(从) 192.168.200.137(从) 并准备好服务器看看装没装gcc:rpm -q gcc 没装的话就装(依次执行): yum install gcc-c++ yum -y instal

m0_37635053的博客 6836

springboot 集成redis_Spring Boot 2.X(六):Spring Boot 集成 Redis

Redis 简介什么是 RedisRedis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库。Redis 与其他 key-value 缓存(如 Memcached )相比有以下三个特点: 1.Redis 支持数据的持久化,它可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。 2.Redis 不仅仅支持简单的 key-value 类型的数据,同时还...

weixin_39637661的博客 242

springboot 集成 redis

1.springboot 集成 redis 1.1 redis数据结构 string<字符串> list<链表> set<集合> zset<有序集合> Hash<哈希类型> redis响应速度快 springboot1.x 中默认客户端时jedis springboot2.x 中默认客户端是lettuce 1.2 引入依赖 <!-- redis starter --> <dependency> <groupId&

weixin_45074962的博客 365

SpringBoot中集成Redis(1)

有了,并于Spring Boot配置文件中定义Redis相关属性后,就会在项目启动时会注入到Redis中去。前面说到spring中为redis定义了专门的自动配置类,其中定义创建泛型为的RedisTemplate对象,为了允许自定义配置Bean,自动配置类使用@ConditionalOnMissingBean注解,表示当其他地方定义一个RedisTemplate的Bean时,会替代自动配置中的结果。​@Bean​@Bean复制代码为了使用操作更方便,我们定义一个。

2401_83916326的博客 1008

spring boot集成redis

一个程序员,只有不断的学习进取才能始终的保持着竞争力。接下来,给大家说说spring boot如何集成redis。 首先我们认识一下redis: redis 是一个高性能的key-value数据库,它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyper...

进步新青年的博客 331

springboot集成redis笔记

大概看图先了解下 1首先导入redis的jar依赖(redis起步依赖) 2在springboot核心配置文件application.properties中配置redis连接信息 示例: 3配置完后springboot将自动配置RedisTemplate,在需要操作redis的类中注入redsiTemplate即可 在要操作的类中注入redis @Autowired private Red...

weixin_43769566的博客 302

SpringBoot集成Redis

以上步骤涵盖了如何在Spring Boot项目中集成Redis,并进行了基本的CRUD操作。你可以根据需要进一步扩展和自定义Redis的配置和使用方式。

2301_78122337的博客 267

SpringBoot工程集成Redis

Redis简介   Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。   Redis支持存储的value类型包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子

LFM10的博客 184

SpringBoot集成redis

关于SpringBoot对于redis的集成,我是为了用redis做token存储验证以及短信验证码登录注册等功能时做缓存使用的 第一步,先下个redis在windows安装好,百度云地址附上 链接:https://pan.baidu.com/s/1x18K9W3yrOB83bSIATTAQw 提取码:bn58 首先:我们要将Redis的pom依赖进行导入 话不多说,上代码 &lt...

白百柏在帝都 204

SpringBoot 整合 Redis:Spring Data Redis(RedisTemplate 的使用)

SpringData 是 Spring 对数据操作的整合模块,它包含对各种数据库的集成。其中 SpringData 对 Redis 的集成模块就叫做 SpringDataRedis。

JiaHao汤 1493
上一篇: 微信分享海报、头像、图片、二维码合成(java)
下一篇: 一次@value取值失败的原因
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值