4.0.0org.springframework.bootspring-boot-starter-parent2.0.8.RELEASEcom.urthink.upfsupfs-provider0.0.1-SNAPSHOTupfs-providerUpfs provider project for Spring Boot1.8Finchley.SR2org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-loggingorg.springframework.bootspring-boot-starter-log4j2org.springframework.bootspring-boot-starter-data-redisorg.apache.commonscommons-pool2org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.projectlomboklomboktruecom.urthink.upfsupfs-model0.0.1-SNAPSHOTorg.springframework.bootspring-boot-maven-plugin
二、application.yml
max-wait和timeout要写单位,否则提示错误redis timeout Value '2000' is not a valid duration
package com.urthink.upfs.model.entity;import lombok.Data;import java.io.Serializable;/*** 实体类*/
@Data
public class User implements Serializable {private static final long serialVersionUID = 1L;private String id;private String name;private int age;}
2.UserService.java,spring boot cache注解
package com.urthink.upfs.provider.service;import com.urthink.upfs.model.entity.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class UserService {@Cacheable(value="user", key="#id") //user::0public User getUser(String id) {System.out.println(id+"进入实现类获取数据!");User user = new User();user.setId(id);user.setName("张三");user.setAge(18);return user;}@CacheEvict(value="user", key="#id", condition="#id!='1'")public void deleteUser(String id) {System.out.println(id+"进入实现类删除数据!");}}
3.RedisService.java,redis 工具类,可以不用
package com.urthink.upfs.provider.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;/*** redis 工具类*/
@Component
public class RedisService {// 在构造器中获取redisTemplate实例, key(not hashKey) 默认使用String类型private RedisTemplate redisTemplate;// 在构造器中通过redisTemplate的工厂方法实例化操作对象private HashOperations hashOperations;private ListOperations listOperations;private ZSetOperations zSetOperations;private SetOperations setOperations;private ValueOperations valueOperations;// IDEA虽然报错,但是依然可以注入成功, 实例化操作对象后就可以直接调用方法操作Redis数据库@Autowiredpublic RedisService(RedisTemplate redisTemplate) {this.redisTemplate = redisTemplate;this.hashOperations = redisTemplate.opsForHash();this.listOperations = redisTemplate.opsForList();this.zSetOperations = redisTemplate.opsForZSet();this.setOperations = redisTemplate.opsForSet();this.valueOperations = redisTemplate.opsForValue();}public void hashPut(String key, HK hashKey, V value) {hashOperations.put(key, hashKey, value);}public Map hashFindAll(String key) {return hashOperations.entries(key);}public V hashGet(String key, HK hashKey) {return hashOperations.get(key, hashKey);}public void hashRemove(String key, HK hashKey) {hashOperations.delete(key, hashKey);}public Long listPush(String key, V value) {return listOperations.rightPush(key, value);}public Long listUnshift(String key, V value) {return listOperations.leftPush(key, value);}public List listFindAll(String key) {if (!redisTemplate.hasKey(key)) {return null;}return listOperations.range(key, 0, listOperations.size(key));}public V listLPop(String key) {return listOperations.leftPop(key);}public void setValue(String key, V value) {valueOperations.set(key, value);}public void setValue(String key, V value, long timeout) {ValueOperations vo = redisTemplate.opsForValue();vo.set(key, value, timeout, TimeUnit.MILLISECONDS);}public V getValue(String key) {return valueOperations.get(key);}public void remove(String key) {redisTemplate.delete(key);}public boolean expire(String key, long timeout, TimeUnit timeUnit) {return redisTemplate.expire(key, timeout, timeUnit);}
}
/*** Creates a default {@link CacheKeyPrefix} scheme that prefixes cache keys with {@code cacheName} followed by double* colons. A cache named {@code myCache} will prefix all cache keys with {@code myCache::}.** @return the default {@link CacheKeyPrefix} scheme.*/static CacheKeyPrefix simple() {return name -> name + "::";}
参考:
spring boot2 集成Redis https://www.cnblogs.com/antball/p/9239663.html