您的当前位置:首页正文

java操作redis指定库存储数据

2024-11-29 来源:个人技术集锦

1.导包

    	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

2.application.properties 中指定库:
指定3号库 驼峰命名 需要和app.config中 private int redisInterceptorDatabase; 相对应

app.redis-Interceptor-database=3   

3.app.config 文件

package leyan.website.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import io.lettuce.core.resource.ClientResources;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.session.data.redis.config.annotation.SpringSessionRedisConnectionFactory;

import java.util.Locale;

@Configuration
@ConfigurationProperties("app")
@EnableConfigurationProperties(AppConfig.class)
@EnableCaching
@Getter
@Setter
public class AppConfig {

    private int redisInterceptorDatabase;


    @Bean
    protected ObjectMapper objectMapper() {
        Locale.setDefault(Locale.US);
        var objectMapper = new ObjectMapper();
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 不准写入值为 null 的属性,防止属性泄露
        return objectMapper;
    }

    @Bean
    protected PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(13);
    }

    @Bean
    protected LettuceClientConfiguration lettuceClientConfiguration(ClientResources clientResources, RedisProperties redisProperties,
                                                                    ObjectProvider<LettuceClientConfigurationBuilderCustomizer> redisBuilderCustomizers) {
        var configurationBuilder = LettuceClientConfiguration.builder();
        if (redisProperties.isSsl()) {
            configurationBuilder.useSsl();
        }
        if (redisProperties.getTimeout() != null) {
            configurationBuilder.commandTimeout(redisProperties.getTimeout());
        }
        if (redisProperties.getLettuce() != null) {
            var lettuce = redisProperties.getLettuce();
            if (lettuce.getShutdownTimeout() != null && !lettuce.getShutdownTimeout().isZero()) {
                configurationBuilder.shutdownTimeout(redisProperties.getLettuce().getShutdownTimeout());
            }
        }
        configurationBuilder.clientResources(clientResources);
        redisBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(configurationBuilder));
        return configurationBuilder.build();
    }


	//bean中名字需要和工厂中@Qualifier("redisConnectionInterceptorFactory") 相对应
    @Bean("redisConnectionInterceptorFactory")
    protected RedisConnectionFactory redisCacheConnectionInterceptorFactory(LettuceClientConfiguration lettuceClientConfiguration, RedisProperties redisProperties) {
        var config = new RedisStandaloneConfiguration();
        config.setHostName(redisProperties.getHost());
        config.setPort(redisProperties.getPort());
        config.setPassword(RedisPassword.of(redisProperties.getPassword()));
        config.setDatabase(redisInterceptorDatabase);
        return new LettuceConnectionFactory(config, lettuceClientConfiguration);
    }


    

    @Bean("redisInterceptorTemplate")
    public RedisTemplate<String, String> redisInterceptorTemplate(@Qualifier("redisConnectionInterceptorFactory") RedisConnectionFactory factoryCart) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factoryCart);

        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
        //om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        jacksonSeial.setObjectMapper(om);

        // 值采用json序列化
        template.setValueSerializer(jacksonSeial);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());

        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();

        return template;
    }
}

package leyan.website.util;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class RedisInterceptorUtils {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(RedisInterceptorUtils.class);
    @Autowired
    @Qualifier("redisInterceptorTemplate")//@Qualifier("redisInterceptorTemplate") 需要和app.config中配置redis指定工程名称一致
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 读取缓存
     */
    public String get(final String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 写入缓存
     */
    public boolean set(final String key, String value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新缓存
     */
    public boolean getAndSet(final String key, String value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 删除缓存
     */
    public boolean delete(final String key) {
        boolean result = false;
        try {
            if (key != null && key.length() > 0) {
                redisTemplate.delete(key);
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 指定缓存失效时间
     */
    public void expire(String key, long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
        } catch (Exception e) {
            logger.error("RedisUtils expire(String key,long time) failure." + e.getMessage());
        }
    }

    /**
     * 将list放入缓存
     */
    public void listSet(String key, String value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) {
                expire(key, time);
            }
        } catch (Exception e) {
            logger.error("RedisUtils lSet(String key, Object value, long time) failure." + e.getMessage());
        }
    }

    /**
     * 根据索引修改list中的某条数据
     */
    public void listUpdateIndex(String key, long index, String value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
        } catch (Exception e) {
            logger.error("RedisUtils lUpdateIndex(String key, long index,Object value) failure." + e.getMessage());
        }
    }

    /**
     * 获取list缓存的内容
     */
    public List<String> listGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            logger.error("RedisUtils lGet(String key, long start, long end) failure." + e.getMessage());
            return null;
        }
    }

    /**
     * 移除N个值为value
     */
    public long deleteList(String key, long count, Object value) {
        try {
            return redisTemplate.opsForList().remove(key, count, value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * HashSet 并设置时间
     */

    public boolean setHash(String key, Map<String, Object> map, long time) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取hashKey对应的所有键值
     */

    public Map<Object, Object> getHash(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * HashGet
     */
    public Object getOneHash(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    /**
     * 删除hash表中的值
     */
    public void deleteHash(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }



    /**
     * 判断redis是否存在
     */
    public boolean exitsKey(String key) {
        return redisTemplate.hasKey(key);
    }
}

显示全文