当前位置: 首页 > news >正文

临海市网站建设爱站工具包

临海市网站建设,爱站工具包,做网站公司赚钱,b2b网站建设报价文章目录一、Jedis二、Spring Data Redis(常用)【1】pom.xml【2】application.yml【3】RedisConfig【4】RuiJiWaiMaiApplicationTests三、Spring Cache【1】常用注解:【2】使用案例【3】底层不使用redis,重启服务,内存…

文章目录

        • 一、Jedis
        • 二、Spring Data Redis(常用)
            • 【1】pom.xml
            • 【2】application.yml
            • 【3】RedisConfig
            • 【4】RuiJiWaiMaiApplicationTests
        • 三、Spring Cache
          • 【1】常用注解:
          • 【2】使用案例
          • 【3】底层不使用redis,重启服务,内存丢失=>解决:
            • pom.xml
            • application.yml
            • 启动类:
            • Result:
            • 注解使用:


一、Jedis

在这里插入图片描述

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>
</dependency>
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.8.0</version>
</dependency>

在这里插入图片描述

二、Spring Data Redis(常用)

【1】pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
【2】application.yml

在这里插入图片描述

spring:redis:    #redis配置host: localhostport: 6379#password:database: 0   #默认提供16个数据库,0:0号数据库jedis:pool:  #redis连接池配置max-active: 8   #最大连接数max-idle: 4     #最大空闲连接max-wait: 1ms   #最大阻塞等待时间min-idle: 0     #最小空闲连接

在这里插入图片描述

【3】RedisConfig
package com.example.ruijiwaimai.config;import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();// 默认的key序列化器为:JdkSerializationRedisSerializerredisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(connectionFactory);return redisTemplate;}
}
【4】RuiJiWaiMaiApplicationTests
package com.example.ruijiwaimai;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;@SpringBootTest
class RuiJiWaiMaiApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;/** 操作String类型数据=>ValueOperations:简单K-V操作* */@Testpublic void testString() {redisTemplate.opsForValue().set("city123", "shenzhen");    //  \xac\xed\x00\x05t\x00\x04city做了序列化,无法用get city获取=》config配置RedisConfigString value = (String) redisTemplate.opsForValue().get("city123");System.out.println(value);redisTemplate.opsForValue().set("key1", "value1", 10, TimeUnit.SECONDS);Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "nanjing");System.out.println(aBoolean);}/** 操作Hash类型数据=>HashOperations:针对map类型的数据操作* */@Testpublic void testHash() {HashOperations hashOperations = redisTemplate.opsForHash();hashOperations.put("002", "name", "xiaoming");hashOperations.put("002", "age", "20");String age = (String) hashOperations.get("002", "age");System.out.println(age);// 获取所有字段Set keys = hashOperations.keys("002");for (Object key : keys) {System.out.println(key);}// 获取所有值List values = hashOperations.values("002");for (Object value : values) {System.out.println(value);}}/** 操作List类型数据=>ListOperations:针对list类型的数据操作* */@Testpublic void testList() {ListOperations listOperations = redisTemplate.opsForList();//存值listOperations.leftPush("mylist", "a");listOperations.leftPushAll("mylist", "b", "c", "d");//取值List<String> mylist = listOperations.range("mylist", 0, -1);for (String value : mylist) {System.out.println(value);}//获得列表长度Long size = listOperations.size("mylist");int lSize = size.intValue();for (int i = 0; i < lSize; i++) {//出队列Object element = listOperations.rightPop("mylist");System.out.println("出队列:" + element);}}/** 操作Set(无序集合)类型数据=>SetOperations:set类型数据操作* */@Testpublic void testSet() {SetOperations setOperations = redisTemplate.opsForSet();//存值setOperations.add("myset", "a", "b", "c", "a");//取值Set<String> myset = setOperations.members("myset");for (String o : myset) {System.out.println(o);}//删除成员setOperations.remove("myset", "a", "b");myset = setOperations.members("myset");for (String o : myset) {System.out.println("删除后的数据:" + o);}}/** 操作ZSet(有序集合)类型数据=>ZSetOperations:zset类型数据操作* */@Testpublic void testZSet() {ZSetOperations zSetOperations = redisTemplate.opsForZSet();//存值zSetOperations.add("myZset", "a", 10.0);zSetOperations.add("myZset", "b", 11.0);zSetOperations.add("myZset", "c", 12.0);zSetOperations.add("myZset", "a", 13.0);//取值Set<String> myZet = zSetOperations.range("myZset", 0, -1);for (String s : myZet) {System.out.println(s);}//修改分数zSetOperations.incrementScore("myZset", "b", 20.0);myZet = zSetOperations.range("myZset", 0, -1);for (String s : myZet) {System.out.println("修改分数: " + s);}//删除成员zSetOperations.remove("myZset", "a", "b");myZet = zSetOperations.range("myZset", 0, -1);for (String s : myZet) {System.out.println("删除后的成员: " + s);}}/** 通用操作* */@Testpublic void testCommon() {//获取redis中所有的keySet keys = redisTemplate.keys("*");for (Object key : keys) {System.out.println(key);}//判断某个key是否存在Boolean itcast = redisTemplate.hasKey("itcast");System.out.println("判断某个key是否存在:"+itcast);//删除指定keyredisTemplate.delete("myZset");//获取指定key对应的value的数据类型DataType dataType = redisTemplate.type("001");System.out.println("获取指定key对应的value的数据类型:"+dataType.name());}
}

在这里插入图片描述

三、Spring Cache

【1】常用注解:
注解说明
@EnableCaching开启缓存注解功能
@Cacheable判断是否有缓存数据,有=》返回缓存数据;没有=》放到缓存中
@CachePut将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除
【2】使用案例
package com.itheima.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {@Autowiredprivate CacheManager cacheManager;@Autowiredprivate UserService userService;/*** CachePut:将方法返回值放入缓存* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key*/@CachePut(value = "userCache",key = "#user.id")@PostMappingpublic User save(User user){userService.save(user);return user;}/*** CacheEvict:清理指定缓存* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key*/@CacheEvict(value = "userCache",key = "#p0")//@CacheEvict(value = "userCache",key = "#root.args[0]")//@CacheEvict(value = "userCache",key = "#id")@DeleteMapping("/{id}")public void delete(@PathVariable Long id){userService.removeById(id);}//@CacheEvict(value = "userCache",key = "#p0.id")//@CacheEvict(value = "userCache",key = "#user.id")//@CacheEvict(value = "userCache",key = "#root.args[0].id")@CacheEvict(value = "userCache",key = "#result.id")@PutMappingpublic User update(User user){userService.updateById(user);return user;}/*** Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key* condition:条件,满足条件时才缓存数据* unless:满足条件则不缓存*/@Cacheable(value = "userCache",key = "#id",unless = "#result == null")@GetMapping("/{id}")public User getById(@PathVariable Long id){User user = userService.getById(id);return user;}@Cacheable(value = "userCache",key = "#user.id + '_' + #user.name")@GetMapping("/list")public List<User> list(User user){LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(user.getId() != null,User::getId,user.getId());queryWrapper.eq(user.getName() != null,User::getName,user.getName());List<User> list = userService.list(queryWrapper);return list;}
}
【3】底层不使用redis,重启服务,内存丢失=>解决:
pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
application.yml

在这里插入图片描述

  redis:    #redis配置host: 192.168.139.128port: 6379#password:database: 0   #默认提供16个数据库,0;0号数据库cache:redis:time-to-live: 1800000 #设置缓存数据的过期时间
启动类:

在这里插入图片描述

Result:

在这里插入图片描述

注解使用:
    @PostMapping()@CacheEvict(value = "setmealCache",allEntries = true)public Result<String> save(@RequestBody SetmealDto setmealDto) {log.info("套餐信息:{}", setmealDto);setmealService.saveWithDish(setmealDto);return null;}//删除套餐@DeleteMapping()@CacheEvict(value = "setmealCache",allEntries = true)//清除setmealCache下的所有缓存public Result<String> delete(@RequestParam List<Long> ids){setmealService.removeWithDish(ids);return Result.success("套餐数据删除成功");}/*** 根据条件查询套餐数据* @param setmeal* @return*/@GetMapping("/list")@Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")public Result<List<Setmeal>> list(Setmeal setmeal){LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());queryWrapper.orderByDesc(Setmeal::getUpdateTime);List<Setmeal> list = setmealService.list(queryWrapper);return Result.success(list);}
http://www.hkea.cn/news/447205/

相关文章:

  • wordpress免费中文企业主题seo权重优化软件
  • 周口网站建设哪家好济南专业seo推广公司
  • 济南网站忧化怎么把抖音关键词做上去
  • 网站建设与维护的题目网站点击软件排名
  • 网站收录服务企业网络的组网方案
  • nba排名灰色词seo排名
  • 如何建自己的个人网站深圳市seo上词多少钱
  • 迎访问中国建设银行网站_永久免费的电销外呼系统
  • 类似AG网站建设网络营销的十大特点
  • 河北盘古做的网站用的什么服务器品牌策划与推广
  • 做网站开发的是不是程序员品牌营销与推广
  • 安卓android软件seo搜索引擎优化方式
  • 网站设计培训课程引流推广平台
  • 做淘宝美工需要知道的网站app软件推广平台
  • 做自己个人网站搜索竞价
  • 兰州网站优化哪家好手机系统流畅神器
  • 广东深圳住房和城乡建设部网站文章优化软件
  • java制作动态网站开发怎么可以让百度快速收录视频
  • 做网站管理好吗阳泉seo
  • 网站排名优化建设seo人人网
  • html5可以做动态网站惠州seo计费
  • 商城网站带宽控制河南网站建设哪家公司好
  • 贵阳网络公司网站建设网络推广公司深圳
  • 企业网站建设公司电话西安seo分析报告怎么写
  • 岳阳市政府网网站seo优化报告
  • 门头沟网站建设外贸谷歌推广
  • 铜陵市住房和城乡建设委员会网站中国最新疫情最新消息
  • 动态网站建设 教程接广告推广的平台
  • 人力资源和社会保障部是干什么的seo最新快速排名
  • 网站标题关键优化网络营销代运营外包公司