zoukankan      html  css  js  c++  java
  • SpringBoot 中使用redis以及redisTemplate

    1.首先在pom.xml中添加依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>1.5.2.RELEASE</version>
    </dependency>

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

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

    2.在git上的配置文件中添加redis集群配置

    spring.redis.cluster.nodes=10.10.10.236:7001,10.10.10.236:7002,10.10.10.236:7003,10.10.10.236:7004,10.10.10.236:7005,10.10.10.236:7006
    spring.redis.cluster.host=10.10.10.236
    spring.redis.cluster.port=7001
    spring.redis.cluster.passwd=
    spring.redis.cluster.timeOut=2000
    spring.redis.cluster.max-redirects=8

    3.在程序中读取配置文件中的值

    package cn.lz.auth.redis;

    import java.util.HashMap;
    import java.util.Map;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.MapPropertySource;
    import org.springframework.data.redis.connection.RedisClusterConfiguration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.SerializationException;
    import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

    //@Component
    @EnableRedisHttpSession
    @Configuration
    public class RedisConfig
    {
    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;

    @Value("${spring.redis.cluster.host}")
    private String redisHost;

    @Value("${spring.redis.cluster.port}")
    private int redisPort;

    @Value("${spring.redis.cluster.passwd}")
    private String redisPasswd;

    @Value("${spring.redis.cluster.timeOut}")
    private int timeOut = 2000;

    @Value("${spring.redis.cluster.max-redirects}")
    private int redirects = 8;

    @Bean
    public RedisClusterConfiguration getClusterConfiguration()
    {
    Map<String, Object> source = new HashMap<String, Object>();
    source.put("spring.redis.cluster.nodes", clusterNodes);
    source.put("spring.redis.cluster.timeout", timeOut);
    source.put("spring.redis.cluster.max-redirects", redirects);
    return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory()
    {
    JedisConnectionFactory cf = null;
    if( clusterNodes != null && !clusterNodes.isEmpty() ){
    cf = new JedisConnectionFactory( getClusterConfiguration() );
    }
    else{
    cf = new JedisConnectionFactory();
    cf.setHostName( redisHost );
    cf.setPort( redisPort );
    cf.setPassword( redisPasswd);
    cf.setTimeout( timeOut );
    }

    cf.afterPropertiesSet();
    return cf;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate()
    {
    RedisTemplate<String, Object> rt = new RedisTemplate<String, Object>();
    rt.setConnectionFactory( redisConnectionFactory() );
    return rt;
    }

    public static enum StringSerializer implements RedisSerializer<String>
    {
    INSTANCE;

    @Override
    public byte[] serialize(String s) throws SerializationException
    {
    return (null != s ? s.getBytes() : new byte[0]);
    }

    @Override
    public String deserialize(byte[] bytes) throws SerializationException
    {
    if (bytes.length > 0){
    return new String(bytes);
    }
    else{
    return null;
    }
    }
    }

    public static enum LongSerializer implements RedisSerializer<Long>
    {
    INSTANCE;

    @Override
    public byte[] serialize(Long aLong) throws SerializationException
    {
    if (null != aLong){
    return aLong.toString().getBytes();
    }
    else{
    return new byte[0];
    }
    }

    @Override
    public Long deserialize(byte[] bytes) throws SerializationException
    {
    if (bytes.length > 0){
    return Long.parseLong(new String(bytes));
    }
    else{
    return null;
    }
    }
    }


    public static enum IntSerializer implements RedisSerializer<Integer>
    {
    INSTANCE;

    @Override
    public byte[] serialize(Integer i) throws SerializationException
    {
    if (null != i){
    return i.toString().getBytes();
    }
    else{
    return new byte[0];
    }
    }

    @Override
    public Integer deserialize(byte[] bytes) throws SerializationException
    {
    if (bytes.length > 0){
    return Integer.parseInt(new String(bytes));
    }
    else{
    return null;
    }
    }
    }

    }

    4.根据redisTemplate编写set,get,delete方法

    package cn.lz.auth.redis;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.stereotype.Component;

    @Component
    public class RedisService<T>
    {
    @Autowired
    private RedisTemplate<String, Object> redis;

    public void setObject( String uuid, T object, Class<T> type )
    {
    redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
    redis.setValueSerializer( new Jackson2JsonRedisSerializer<T>(type) );
    redis.afterPropertiesSet();

    ValueOperations<String, Object> ops = redis.opsForValue();
    ops.set( uuid, object );
    }

    @SuppressWarnings("unchecked")
    public T getObject( String uuid, Class<T> type )
    {
    redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
    redis.setValueSerializer( new Jackson2JsonRedisSerializer<T>(type) );
    redis.afterPropertiesSet();

    ValueOperations<String, Object> ops = redis.opsForValue();
    return (T)ops.get( uuid );
    }

    public void delObject( String uuid )
    {
    redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
    redis.afterPropertiesSet();

    redis.delete( uuid );
    }
    }

     5.调用set,get,delete方法操作redis中的数据

    好文链接:http://www.cnblogs.com/langtianya/p/5190201.html

  • 相关阅读:
    使用 MDT 2010 进行可伸缩部署
    Windows Phone 7 WebBrowser 中文乱码问题
    如何安装Windows Phone SDK 7.1 Release Candidate (RC)
    Windows Phone应用 博客园阅读器
    将WindowsPhoneApp部署到HTC Surround,兄弟们支个招如何进行Debug
    私有代码存放仓库 BitBucket
    入门:添加一个支持获取单一资源以及支持POST,PUT和DELETE方法
    Html5 学习利器 Web Standards Update for Microsoft Visual Studio 2010 SP1
    在启用了IPV6的机器上获取客户端ipv4地址
    EMA算法的C#实现
  • 原文地址:https://www.cnblogs.com/yuxiaona/p/7160648.html
Copyright © 2011-2022 走看看