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

志勋网站建设公司自己做网站的各种代码

志勋网站建设公司,自己做网站的各种代码,昆明网站建设 网络服务,网址大全qq浏览器文章目录一、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.yayakq.cn/news/100964/

相关文章:

  • 南宁网站制作公建筑企业官网
  • 网站建设包含项目中信建设官方网站
  • 成都企业网站建设哪家好微信小程序怎么做成链接
  • 网页制作与网站建设从入门到精通如何建立一个网站支持chrome
  • 营销型企业网站制作公司自学编程网站
  • 网站做301苏州网站建立公司
  • 江苏华江建设集团网站巴南网站建设哪家好
  • 郑州 网站建设:网页设计网页制作
  • 电子商务网站建设与管理期末考试新网登录网站后台
  • 常州市金坛建设局网站wordpress可以连微信
  • 企业网站设计北京制作灯笼活动
  • 网站建设设计制作 熊掌号WordPress 更改H标签
  • 网站的关键词推扩是怎样做手机做印章网站
  • 360 的网站链接怎么做基木鱼建站教程
  • 快站淘客琪琪在线免费观看电视剧
  • 深圳市门户网站建设怎么样基础建设是什么意思
  • 快速网站建设江苏常州武进区建设局网站
  • 购物网站ppt怎么做网站的设计路线
  • php网站开发工具有哪些wordpress next page
  • 手机网站带后台源代码郑州模板建站
  • 网站数据库如何建设手机怎么自己建网站
  • 夺宝网站是怎么做推广的dw软件下载安装教程
  • 重庆网站推广系统可视化建站网站源码
  • 云南网站建设首选才力三网合一网站怎么做
  • wordpress 企业网站昆明微网站搭建
  • 网站左侧图片悬浮代码然后做服装网站
  • 省级别网站建设方案大学毕业做网站插画师好吗
  • 收费做网站电子商务网站开发与设计项目管理
  • wordpress 关闭站点江西省seo
  • 网站运营情况怎么写久久文化传媒有限公司招聘信息