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

医美行业网站建设广州网站建设公司排名

医美行业网站建设,广州网站建设公司排名,网站集群建设相关的招标,天津协会网站建设个人简介#xff1a;Java领域新星创作者#xff1b;阿里云技术博主、星级博主、专家博主#xff1b;正在Java学习的路上摸爬滚打#xff0c;记录学习的过程~ 个人主页#xff1a;.29.的博客 学习社区#xff1a;进去逛一逛~ Jedis、SpringDataRedis、StringRedisTemplate… 个人简介Java领域新星创作者阿里云技术博主、星级博主、专家博主正在Java学习的路上摸爬滚打记录学习的过程~ 个人主页.29.的博客 学习社区进去逛一逛~ Jedis、SpringDataRedis、StringRedisTemplate Redis的Java客户端使用Jedis快速入门Jedis连接池SpringDataRedis快速入门自定义RedisTemplate的序列化方式StringRedisTemplate序列化 Redis的Java客户端使用 Jedis快速入门 引入依赖 dependencies!--Redis的Java客户端Jedis 相关依赖--dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion4.3.0/version/dependency!--单元测试依赖--dependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter/artifactIdversion5.9.2/versionscopetest/scope/dependency/dependencies测试Java客户端操作Redis 测试代码 import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import redis.clients.jedis.Jedis;import java.util.Map;/*** author .29.* create 2023-05-08 20:24*/public class JedisTest {private Jedis jedis;//链接RedisBeforeEachvoid setUp(){//1.建立连接jedis new Jedis(192.168.88.128,6379);//参数ip地址、端口号//2.设置密码jedis.auth(123456);//3.选择库jedis.select(0);}//测试java客户端操作RedisString类型操作Testpublic void test1(){//存入数据String result jedis.set(name, .29.);System.out.println(result result);//获取数据String name jedis.get(name);System.out.println(name name);}//测试java客户端操作RedisHash类型操作Testpublic void test2(){//存入数据jedis.hset(user:1,name,Little29);jedis.hset(user:1,age,19);//获取数据MapString, String result jedis.hgetAll(user:1);System.out.println(result);}//关闭资源AfterEachvoid tearDown(){if(jedis ! null){jedis.close();}} }测试结果 ⚪—操作String类型—⚪ ⚪—操作hash类型—⚪ Jedis连接池 为什么使用Jedis连接池 Jedis本身是线程不安全 的并且频繁创建和销毁连接会有性能损耗 因此推荐大家使用Jedis连接池代替Jedis的直连 方式。 Jedis连接池——配置工具类 import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.time.Duration;/*** author .29.* create 2023-05-08 20:47*/ public class JedisConnectionFactory {//jedis连接池对象private static final JedisPool jedisPool;static {JedisPoolConfig jedisPoolConfig new JedisPoolConfig();//最大连接jedisPoolConfig.setMaxTotal(8);//最大空闲连接jedisPoolConfig.setMaxIdle(8);//最小空闲连接jedisPoolConfig.setMinIdle(0);//设置最长等待时间单位msjedisPoolConfig.setMaxWait(Duration.ofMillis(1000));//jedisPoolConfig.setMaxWaitMillis(1000);//较早版本方式//参数连接池配置、ip地址、端口号、超时时间、密码jedisPool new JedisPool(jedisPoolConfig, 192.168.88.128,6379,1000,123456);}//获取Jedis对象public static Jedis getJedis(){return jedisPool.getResource();} }SpringDataRedis快速入门 SpringDataRedis简介 SpringData是Spring中数据操作的模块包含对各种数据库的集成其中对Redis的集成模块就叫做SpringDataRedis官网网址https://spring.io/projects/spring-data-redis 功能介绍 提供了对不同Redis客户端的整合Lettuce和Jedis提供RedisTemplate统一API来操作Reids支持Redis的发布订阅模型支持Reids哨兵和Redis集群支持基于Lettuce的响应式编程支持基于JDK、JSON、字符串、Spring对象的数据序列化和反序列化支持基于Redis的JDKCollection实现 引入依赖(需要是SpringBoot工程) !--Redis依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!--连接池依赖--dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependencyapplication.yml配置 spring:redis:host: 192.168.88.128password: 123456port: 6379lettuce:pool:max-active: 8 #最大连接max-idle: 8 #最大空闲连接max-wait: 100 #连接等待时间min-idle: 0 #最小空闲连接注入RedisTemplate编写测试 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.core.RedisTemplate;SpringBootTest class SpringDataRedisDemoApplicationTests {//注入Autowiredprivate RedisTemplate redisTemplate;Testvoid contextLoads() {//写入一条String数据redisTemplate.opsForValue().set(age,19);//获取String数据Object age redisTemplate.opsForValue().get(age);System.out.println(age age);}}SpringDataRedis的序列化方式 RedisTemplate可以接收任意Object作为值写入Redis只不过写入前会把Object序列化成字节形式默认是采用JDK序列化。但是此方式得到的结果可读性差内存占用大缺点 自定义RedisTemplate的序列化方式 自定义序列化 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.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer;/*** author .29.* create 2023-05-09 16:12*/ Configuration public class RedisConfig {BeanConditionalOnSingleCandidatepublic RedisTemplateString,Object redisTemplate(RedisConnectionFactory connectionFactory){//创建RedisTemplate对象RedisTemplateString,Object redisTemplate new RedisTemplate();//设置连接工厂redisTemplate.setConnectionFactory(connectionFactory);//创建JSON序列化工具GenericJackson2JsonRedisSerializer jsonRedisSerializer new GenericJackson2JsonRedisSerializer();//设置Key序列化(String类型)redisTemplate.setKeySerializer(RedisSerializer.string());redisTemplate.setHashKeySerializer(RedisSerializer.string());//设置value序列化(JSON格式)redisTemplate.setValueSerializer(jsonRedisSerializer);redisTemplate.setHashValueSerializer(jsonRedisSerializer);//返回return redisTemplate;} }注意 需要导入SpringMVC依赖或Jackson依赖 Jackson依赖(SpringBoot项目无须手动指定版本号) dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactId/dependency测试 SpringBootTest class SpringDataRedisDemoApplicationTests {//注入Resourceprivate RedisTemplateString,Object redisTemplate;//测试操作RedisTestvoid contextLoads() {//写入一条String数据redisTemplate.opsForValue().set(age,19);redisTemplate.opsForValue().set(name,自定义姓名);//获取String数据Object age redisTemplate.opsForValue().get(age);Object name redisTemplate.opsForValue().get(name);System.out.println(age age);System.out.println(name name);}}注意 JSON的序列化方式满足我们的需求单仍然存在问题为了在反序列化时知道对象的类型JSON序列化器会将类的class类型写入json结果中存入Redis会带来额外的内存开销。为了节省空间我们并不会使用JSON序列化器来处理value而是统一使用String序列化器要求只存储String类型的key和value。当需要存储java对象时手动完成对象的序列化和反序列化。 StringRedisTemplate序列化 Spring默认提供了一个StringRedisTemplate类它的key和value的系列化默认方式为String方式省去自定义RedisTemplate的过程。 示例 import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; 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.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;import javax.annotation.Resource; import java.util.Map;SpringBootTest class RedisDemoApplicationTests {//使用StringRedisTemplate手动进行序列化与反序列化Resourceprivate StringRedisTemplate stringRedisTemplate;//JSON工具private static final ObjectMapper mapper new ObjectMapper();Testpublic void StringRedisTemplateTest() throws JsonProcessingException {//设置对象User user new User(name3, 29);//手动序列化String set mapper.writeValueAsString(user);//向Redis写入数据stringRedisTemplate.opsForValue().set(user:3,set);//向Redis获取数据String get stringRedisTemplate.opsForValue().get(user:3);//手动反序列化User value mapper.readValue(get, User.class);System.out.println(user:3 value);}Testpublic void testHash(){//向Redis存入Hash键值对stringRedisTemplate.opsForHash().put(user:4,HashName,name4);//向Redis获取Hash键值对MapObject, Object entries stringRedisTemplate.opsForHash().entries(user:4);System.out.println(entries);} }
http://www.huolong8.cn/news/129077/

相关文章:

  • 上海网站搜索排名网页设计步骤及方法
  • 网络推广员是什么德州哪家网站优化好
  • 佛山网站优化什么价格佛山做网站业务工资
  • 微信网站在线登录网页版培训心得体会范文大全1000
  • 电商运营有几大平台自己的网站做优化怎么设置缓存
  • 网站建设优化课程公司网站作用
  • 优势的seo网站优化排名化妆品备案
  • 山西省网站制作北京网页设计制作
  • 沈阳模板 网站建设网站挑错
  • 鄂州市 网站建设新乡做网站的公司有那些
  • 如何做网站的维护和推广推广网店的途径和方法
  • 多个招聘网站格式不一致如何做招聘记录绿盒子网站建设案例
  • 建网站网络公司怎么做推广网站赌场
  • py网站开发视频教程棋牌游戏开发
  • 昆山网站制作互联网推广运营是干什么的
  • 河北永生建筑工程网站网站加一个会员登陆怎么做
  • 咋样做网站视频黄陂机械加工网
  • 网站搬家教程电脑网页制作培训
  • 上海备案证查询网站查询网站查询wordpress appdev team
  • 英德住房和城乡建设部网站莆田城市投资建设集团网站
  • 网站开发合同范本下载岳溥庥网站建设
  • iis怎么做ip网站吗page文件怎么转换wordpress
  • 淮安市网站百度权重优化软件
  • asp网站部署 iis7wordpress获取文章评论数
  • ios手机网站建设在word环境下wordpress
  • 设置网站关键词怎么做淘宝宝贝关键词排名查询工具
  • 网站开发网页页面跳转企业网站建设规划ppt
  • 七色板网站建设dns网站卫士 收录
  • 网站布局策划的流程图和田网页设计
  • 上海集团网站建设咨询成都房地产最新政策