• 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

  • 相关阅读:
    Sql Server截断日志(转)
    C#/VB.NET语法的比较(转)
    ReportViewer矩阵报表
    逐步学习 iPhone App 开发(1)
    一月二十四日,无家可归
    再见2009
    poj 1376 机器人广搜
    hdu 1004 颜色问题
    hdu 2734 简单地字符串处理
    1116 贪婪的送礼者
  • 原文地址:https://www.cnblogs.com/yuxiaona/p/7160648.html
Copyright © 2020-2023  润新知