springboot redis 示例代碼

pom.xmlhtml

<?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.propertiesjava

##端口號
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"; } } }
相關文章
相關標籤/搜索