• springboot redis 示例代码


    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.ts</groupId>
        <artifactId>test_redis</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>test_redis</name>
        <description>springboot_redis</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>

    application.properties

    ##端口号
    server.port=8888
    
    # Redis数据库索引(默认为0)
    spring.redis.database=0 
    # Redis服务器地址
    spring.redis.host=localhost
    # Redis服务器连接端口
    spring.redis.port=6379 
    # Redis服务器连接密码(默认为空)
    spring.redis.password=
    #连接池最大连接数(使用负值表示没有限制)
    spring.redis.pool.max-active=8 
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.pool.max-wait=-1 
    # 连接池中的最大空闲连接
    spring.redis.pool.max-idle=8 
    # 连接池中的最小空闲连接
    spring.redis.pool.min-idle=0 
    # 连接超时时间(毫秒)
    spring.redis.timeout=0
    
    Application.java
    package com.ts;
    
    import java.util.Arrays;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
    RedisConfig.java
    package com.ts;
    
    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    
    /**
     * @author dalaoyang
     * @Description
     * @project springboot_learn
     * @package com.dalaoyang.config
     * @email yangyang@dalaoyang.cn
     * @date 2018/4/18
     */
    @Configuration
    @EnableCaching//开启缓存
    public class RedisConfig extends CachingConfigurerSupport {
    
        @Bean
        public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
            CacheManager cacheManager = new RedisCacheManager(redisTemplate);
            return cacheManager;
        }
    
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
            redisTemplate.setConnectionFactory(factory);
            return redisTemplate;
        }
    }
    RedisService.java
    package com.ts;
    
    import javax.annotation.Resource;
    
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.stereotype.Service;
    
    /**
     * @author dalaoyang
     * @Description
     * @project springboot_learn
     * @package com.dalaoyang.service
     * @email yangyang@dalaoyang.cn
     * @date 2018/4/18
     */
    @Service
    public class RedisService {
        @Resource
        private RedisTemplate<String,Object> redisTemplate;
    
        public void set(String key, Object value) {
            //更改在redis里面查看key编码问题
            RedisSerializer redisSerializer =new StringRedisSerializer();
            redisTemplate.setKeySerializer(redisSerializer);
            ValueOperations<String,Object> vo = redisTemplate.opsForValue();
            vo.set(key, value);
        }
    
        public Object get(String key) {
            ValueOperations<String,Object> vo = redisTemplate.opsForValue();
            return vo.get(key);
        }
    }
    HelloController.java

    package com.ts;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    
    @Controller
    public class HelloController {
    
        @Autowired
        RedisService redisService;
    
        @ResponseBody
        @RequestMapping(value = "/hello",method = RequestMethod.GET)
        public Object hello(String id,String data) {
            Object val = redisService.get("id");
            if (val!= null){
                return val.toString();
            }else{
                redisService.set("id",data);
                return "set success";
            }
        }
    }


  • 相关阅读:
    配置sql server 2000以允许远程访问
    SQLServer大数据量插入BULK INSERT
    【项目经理之修炼(5)】《基础篇》别把项目成功当目标(转)
    C#XML文件操作类
    winform窗体总在所有窗体最上层
    配置VSS2005的Internet访问(转)
    U盘引导盘制作
    【项目经理之修炼(4)】《基础篇》故事的主角是你吗?(转)
    SQLServer收缩数据库日志
    【项目经理之修炼(1)】《序章》关于要写给谁看的问题(转)
  • 原文地址:https://www.cnblogs.com/oftenlin/p/11814361.html
Copyright © 2020-2023  润新知