Redis 能够存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串)、List(列表)、Set(集结)、Hash(散列)和 Zset(有序集结)
String 能够是字符串、整数或许浮点数 对整个字符串或许字符串的其间一部分实行操作;政策和浮点数实行自增(increment)或许自减(decrement)
List 一个链表,链表上的每个节点都包含了一个字符串 从链表的两头推入或许弹出元素;根据偏移量对链表进行修剪(trim);读取单个或许多个元素;根据值来查找或许移除元素
Set 包含字符串的无序收集器(unorderedcollection),而且被包含的每个字符串都是绝无仅有的、各不相同 添加、获取、移除单个元素;检查一个元素是否存在于某个集结中;核算交集、并集、差集;从集结里卖弄随机获取元素
Hash 包含键值对的无序散列表 添加、获取、移除单个键值对;获取全部键值对
Zset 字符串成员(member)与浮点数分值(score)之间的有序映射,元素的摆放次第由分值的大小决议 添加、获取、删去单个元素;根据分值范围(range)或许成员来获取元素
留心: Java bean 要序列化
A8站源码交易平台
1.导入依靠
//gradle
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.1.3.RELEASE'
//pom
org.springframework.boot
spring-boot-starter-data-redis
2.1.3.RELEASE
2.配备redis
A8站源码交易平台
redis.config
@Configuration
public class RedisConfig {
//用于解决注解操作redis 序列话的问题
@Bean(name = "myCacheManager")
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
RedisSerializerjsonSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.SerializationPairpair = RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer);
RedisCacheConfiguration defaultCacheConfig= RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair);
defaultCacheConfig.entryTtl(Duration.ofMinutes(30));
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
/**
* 解决用redisTemplate操作的序列化的问题
*
* @param factory RedisConnectionFactory
* @return redisTemplate
*/
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate
// 配备redisTemplate
RedisTemplate
redisTemplate.setConnectionFactory(factory);
// key序列化
redisTemplate.setKeySerializer(STRING_SERIALIZER);
// value序列化
redisTemplate.setValueSerializer(JACKSON__SERIALIZER);
// Hash key序列化
redisTemplate.setHashKeySerializer(STRING_SERIALIZER);
// Hash value序列化
redisTemplate.setHashValueSerializer(JACKSON__SERIALIZER);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
启动类配备
@EnableCaching //答应注解操作缓存
@SpringBootApplication
public class ShoppingMallApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingMallApplication.class, args);
}
}
application.yml 配备
#redis 缓存配备
redis:
database: 0
host: @ip
port: 6379
timeout: 8000
#假如没有可不写
password:
jedis:
pool:
#衔接池最大衔接数量
max-active: 10
#衔接池最大阻塞时刻
max-wait: -1
#衔接池最小空闲衔接
min-idle: 0
#衔接池最大空闲衔接
max-idle: 8
3.缓存注解介绍
将方法的工作成果进行缓存,以后要相同的数据,直接从缓存中获取 cacheNames 和 key 都必须填,假如不填 key ,默许的 key 是当时的方法名,更新缓存时会由于方法名不同而更新失败
A8站源码交易平台
value: 指定缓存称谓 可指定多个(数组)
key: 缓存数据运用的key默认为方法参数支撑 spEL
keyGenerator: key的生成器
CacheManager: 指定缓存管理器,或获取解析器,两者二选一
condition: 指定条件缓存
unless: 否定缓存. 当unless条件为true时,返回值就不会被缓存,可获取到成果判别
sync: 是否异步, 不支撑unless
@Cacheable(cacheNames = "xxx", key = "xxx")
既调用方法有更新缓存修改了数据库某个数据.一起更新缓存,先调用政策方法将政策方法缓存起来,更新时要留心,要和查询时的key相同,否则缓存不会更新,特色和 @Cacheable 相同
@CachePut(cacheNames = "xxx", key = "xxx")
删去缓存
allEntries:是否删去全部字段的缓存
beforeInvocation:是否在方法之前实行
@CacheEvict(cacheNames = "xxx", key = "xxx")
@CacheConfig
可指定公共key的生成战略
公共的cacheNames (value) 能够一致写在类上面 遮掩就不用每缓存一个就起名字了
公共的CacheManager
@CacheConfig(cacheNames = "product")
public class ProductInfoController {
@Caching(cacheable = {@Cacheable}, put = {@CachePut})
public Response<map
//tO do sthing
}
}
Key 也能够动态设置为方法的参数(支撑EL)
@Cacheable(cacheNames = "xxx", key = "#openid")
public ResultVOdetail(@RequestParam("openid") String openid){
//to do sthing
}
复合注解
//查询数据时 先去更新数据 在放到缓存中
@Caching(cacheable = {@Cacheable}, put = {@CachePut})
@Caching(cacheable = {@Cacheable}, put = {@CachePut}, evict = {@CacheEvict})
4.检验
注解版:
@CacheConfig(cacheNames = "shopping-productInfo")//自定义称谓
public class ProductInfoController {
//简略用法
@Caching(cacheable = {@Cacheable}, put = {@CachePut})
public Response<map
//to do sthing
}
}
Redistemplate版:
@Autowired
private RedisTemplate redisTemplate;
@Test
public void demo(){
//常见redis类型数据操作 set zset 未列出
//string 类型数据
redisTemplate.opsForValue().set("test","123");
redisTemplate.opsForValue().get("test") // 输出成果为123
//list 数据类型
redisTemplate.opsForList().rightPushAll("list",new String[]{"1","2","3"});//从右边刺进元素
redisTemplate.opsForList().range("list",0,-1);//获取全部元素
redisTemplate.opsForList().index("listRight",1);//获取下标为2的元素
redisTemplate.opsForList().rightPush("listRiht","1");//从右边刺进 也可从左边插
redisTemplate.opsForList().leftPop("list");//从左边弹出元素 元素弹出将不存在
//hash
redisTemplate.opsForHash().hasKey("redisHash","111");//判别该hash key 是否存在
redisTemplate.opsForHash().put("redisHash","name","111");//存放 hash 数据
redisTemplate.opsForHash().keys("redisHash");//获取该key对应的hash值
redisTemplate.opsForHash().get("redisHash","age");//给定key 获取 hash 值
}
A8站源码交易平台