java Redis数据缓存

java使用Redis数据库缓存


一、java 代码部分


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.CollectionUtils;
public class RedisUtil {

    private static RedisTemplate<String, Object> redisTemplate;
    /**
     * TODO: 初始化RedisTemplate
     */
    public static void initRedisTemplate(final RedisTemplate<String, Object> sprintRedisTemplate) {
        redisTemplate = sprintRedisTemplate;
    }

    /**
     * @param <T>
     * @description 获取 redis中的对象
     * @param
     * @return void
     * @throws
     */
    public static <T> T redisQueryObject(final RedisTemplate<String, Object> redisTemplate, final String key) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        return (T) ops.get(key);
    }
    /**
     * @description 删除redis中指定的key
     * @param
     * @return void
     * @throws
     */
    public static void redisDeleteKey(final RedisTemplate<String, Object> redisTemplate, final String key) {
        redisTemplate.delete(key);
    }

    /**
     * @description 保存对象到redis
     * @param key
     * @return object
     * @throws
     */
    public static void redisSaveObject(final RedisTemplate<String, Object> redisTemplate, final String key,
            final Object object) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.getAndSet(key, object);
    }

    /**
     * @description 保存对象到redis
     * @param key
     * @return object
     * @throws
     */
    public static void redisSaveObject(final RedisTemplate<String, Object> redisTemplate, final String key,
            final Object object, int time) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.set(key, object, time, TimeUnit.MINUTES);
    }

    /**
     * @description redis保存数据到list
     * @param
     * @return void
     * @throws
     */
    public static void redisSaveList(final RedisTemplate<String, Object> redisTemplate, final String key,
            final Object object, int count) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        ops.leftPush(key, object);
        if (0 < count)
            ops.trim(key, 0, count);
    }

    /**
     * @param <T>
     * @description 获取 redis中的list对象
     * @param
     * @return
     * @throws
     */
    public static <T> List<T> redisQueryList(final RedisTemplate<String, Object> redisTemplate, final String key,
            Class<T> claxx) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        List<Object> tempList = ops.range(key, 0, -1);
        if (CollectionUtils.isEmpty(tempList)) {
            return null;
        }

        List<T> resultList = new ArrayList<>();
        for (Object serl : tempList) {
            resultList.add(claxx.cast(serl));
        }
        tempList.clear();
        return resultList;
    }

    /**
     * @description redis 删除列表中的对象
     * @param
     * @return void
     * @throws
     */
    public static void redisDelListValue(final RedisTemplate<String, Object> redisTemplate, final String key,
            final Serializable value) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        ops.remove(key, 0, value);
    }

    /**
     * @param <T>
     * @description 获取 redis中的对象
     * @param
     * @return void
     * @throws
     */
    public static <T> T redisQueryObject(final String key) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        return (T) ops.get(key);
    }

    /**
     * @description 删除redis中指定的key
     * @param
     * @return void
     * @throws
     */
    public static void redisDeleteKey(final String key) {
        redisTemplate.delete(key);
    }

    /**
     * @description 保存对象到redis
     * @param key
     * @return object
     * @throws
     */
    public static void redisSaveObject(final String key, final Object object) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.getAndSet(key, object);
    }

    /**
     * @description 保存对象到redis
     * @param key
     * @return object
     * @throws
     */
    public static void redisSaveObject(final String key, final Object object, int time) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.set(key, object, time, TimeUnit.MINUTES);
    }

    /**
     * @description redis保存数据到list
     * @param
     * @return void
     * @throws
     */
    public static void redisSaveList(final String key, final Object object, int count) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        ops.leftPush(key, object);
        if (0 < count)
            ops.trim(key, 0, count);
    }

    /**
     * @param <T>
     * @description 获取 redis中的list对象
     * @param
     * @return
     * @throws
     */
    public static <T> List<T> redisQueryList(final String key, Class<T> claxx) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        List<Object> tempList = ops.range(key, 0, -1);
        if (CollectionUtils.isEmpty(tempList)) {
            return null;
        }

        List<T> resultList = new ArrayList<>();
        for (Object serl : tempList) {
            resultList.add(claxx.cast(serl));
        }
        tempList.clear();
        return resultList;
    }

    /**
     * @description redis 删除列表中的对象
     * @param
     * @return void
     * @throws
     */
    public static void redisDelListValue(final String key, final Serializable value) {
        ListOperations<String, Object> ops = redisTemplate.opsForList();
        ops.remove(key, 0, value);
    }

}
二、redis.xml 核心配置

<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<description>redis config</description>
			<value>classpath:context-datasource.properties</value>
		</property>
	</bean>
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.pool.maxActive}" />
		<property name="maxIdle" value="${redis.pool.maxIdle}" />
		<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
		<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
	</bean>

	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="usePool" value="true"></property>
		<property name="hostName" value="${redis.ip}" />
		<property name="port" value="${redis.port}" />
		<property name="password" value="${redis.password}" />
		<property name="timeout" value="${redis.timeout}" />
		<property name="database" value="${redis.default.db}"></property>
		<constructor-arg index="0" ref="jedisPoolConfig" />
	</bean>

	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
		<property name="KeySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
		</property>
		<property name="ValueSerializer">
			<bean
				class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
		</property>

		<property name="HashKeySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
		</property>
		<property name="HashValueSerializer">
			<bean
				class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
		</property>

	</bean>

三、系统常量配置 datasource.properties

redis.ip=X.X.X.X
redis.port=6379
redis.password= XXXXX
redis.default.db=0
redis.timeout=30000

redis.pool.maxActive=1024
redis.pool.maxIdle=200
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true

四、使用方法,以设置验证码为例子

1、保存验证码 ,到缓存中

     String mobile = params.getMobile();
     String validCode = RandomUtil.generateString(4);//产生随机四位数验证码
     RedisUtil.redisSaveObject(mobile, validCode, 10);//将验证码保存到数据库中10分钟
2、从缓存中获取验证码

     String validCode = RedisUtil.redisQueryObject(userVo.getMobile());


欢迎大家多多提提意见,我也是刚开始学习,望大家多多指教,Q:768665210



猜你喜欢

转载自blog.csdn.net/ly10265139/article/details/50519371