java redis快速入门_SpringDataRedis快速入门

本文介绍了如何在Java项目中快速整合并使用SpringDataRedis。首先,通过pom文件引入jedis和spring-data-redis依赖。接着,配置redis连接参数如host、port等。然后,在spring配置文件中设置redis连接池。使用枚举定义缓存键,并通过`redisTemplate`存取数据。最后,提供了一个项目启动时自动执行的缓存预热方法。
摘要由CSDN通过智能技术生成

1.pom引入依赖

redis.clients

jedis

2.9.0

org.springframework.data

spring-data-redis

2.0.5.RELEASE

2.新增配置文件 redis-config.properties

redis.host=127.0.0.1

redis.port=6379

redis.pass=

redis.database=0

redis.maxIdle=300

redis.maxWait=3000

maxWait:连接池中连接用完时,新的请求等待时间,毫秒

maxIdle: 最大闲置个数

3.新增spring配置文件applicationContext-redis.xml

xmlns:cache="http://www.springframework.org/schema/cache"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/beans/spring-cache.xsd">

p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>

4.创建枚举用于存放

package com.qingcheng.utlis;

public enum CacheKey {

AD,//广告

SKU_PRICE,//价格

CATEGORY_TREE;//商品分类导航树

}

5.注入redisTemplate使用,此处使用的存入枚举,当然也可以用其他的

redisTemplate.boundValueOps(CacheKey.CATEGORY_TREE).set(categoryTree);

6.启动项目初始化的类调用,当项目启动会自动执行这个方法

@Component

public class Init implements InitializingBean {

@Autowired

private AdService adService;

public void afterPropertiesSet() throws Exception {

System.out.println("缓存预热");

adService.positions();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值