springboot与redis使用(1)

springboot已经通过利用spring-boot-test-autoconfigure,很好地集成了redis,因此在调用redisTemplate时候,直接注入即可,不需要另外写配置类,除非有特殊的业务要求
工具类可以将对象保存到redis中,至于字符串,列表等操作,以后继续写

配置pom.xml

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.3.5.RELEASEversion>
        <relativePath /> 
parent>
<dependency>
       <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-redisartifactId>
dependency> 

redis工具类

import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 
 * @author liangxj
 *
 */
@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 存入redis中的key,以便区分日后可能会有其他系统接入到redis服务器中
     */
    @Value("${redis.application.name}")
    private String redisKey;
    /**
     * redis开关
     */
    @Value("${redis.application.isOpenRedis}")
    private boolean isOpenRedis;

    public void setRedisKey(String redisKey) {
        this.redisKey = redisKey;
    }

    /**
     * 添加
     *
     * @param key
     *            key
     * @param doamin
     *            对象
     * @param expire
     *            过期时间(单位:秒),传入 -1 时表示不设置过期时间
     */
    public void put(String key, Object doamin, long expire) {
        redisTemplate.opsForHash().put(redisKey, key, doamin);
        if (expire != -1) {
            redisTemplate.expire(redisKey, expire, TimeUnit.SECONDS);
        }
    }

    /**
     * 删除
     *
     * @param key
     *            传入key的名称
     */
    public void remove(String key) {
        redisTemplate.opsForHash().delete(redisKey, key);
    }

    /**
     * 查询
     *
     * @param key
     *            查询的key
     * @return
     */
    public Object get(String key) {
        return redisTemplate.opsForHash().get(redisKey, key);
    }

    /**
     * 获取当前redis库下所有对象
     *
     * @return
     */
    public List getAll() {
        return redisTemplate.opsForHash().values(redisKey);
    }

    /**
     * 查询查询当前redis库下所有key
     *
     * @return
     */
    public Set getKeys() {
        return redisTemplate.opsForHash().keys(redisKey);
    }

    /**
     * 判断key是否存在redis中
     *
     * @param key
     *            传入key的名称
     * @return
     */
    public boolean isKeyExists(String key) {
        return redisTemplate.opsForHash().hasKey(redisKey, key);
    }

    /**
     * 查询当前key下缓存数量
     *
     * @return
     */
    public long count() {
        return redisTemplate.opsForHash().size(redisKey);
    }
} 
  

redis 配置

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0  
# Redis服务器地址
pring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379  
# Redis服务器连接密码(默认为空)
spring.redis.password=redis
# 连接池最大连接数(使用负值表示没有限制)
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=3000
#spring.redis.sentinel.master=**
#此处属于给redis配置主从关系,以便分布
#spring.redis.sentinel.nodes= ***.***.***.***:26379,***.***.***.***:26380,***.***.***.***:26381
redis.application.name=qianhaicredit-gateway

你可能感兴趣的:(springboot,redis-入门简单,springboot,redis)