栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java操作redis指定库存储数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java操作redis指定库存储数据

1.导包

    	
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.session
            spring-session-data-redis
        

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 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 redisInterceptorTemplate(@Qualifier("redisConnectionInterceptorFactory") RedisConnectionFactory factoryCart) {
        RedisTemplate 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;
    }
}

    redis工具类 使用该工具类操作对应库
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 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());
        }
    }

    
    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());
        }
    }

    
    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());
        }
    }

    
    public List 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;
        }
    }

    
    public long deleteList(String key, long count, Object value) {
        try {
            return redisTemplate.opsForList().remove(key, count, value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    

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

    

    public Map getHash(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

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

    
    public void deleteHash(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }



    
    public boolean exitsKey(String key) {
        return redisTemplate.hasKey(key);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704125.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号