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

自学做网站可以嘛网站搜索引擎优化方案

自学做网站可以嘛,网站搜索引擎优化方案,dz论坛网站后台设置,人大网站建设情况在Spring Data Redis中,RedisTemplate 是操作Redis的核心类,它提供了丰富的API来与Redis进行交互。由于Redis是一个键值存储系统,它存储的是字节序列,因此在使用RedisTemplate时,需要指定键(Key&#xff09…

在Spring Data Redis中,RedisTemplate 是操作Redis的核心类,它提供了丰富的API来与Redis进行交互。由于Redis是一个键值存储系统,它存储的是字节序列,因此在使用RedisTemplate时,需要指定键(Key)和值(Value)的序列化方式。不同的序列化方式适用于不同的场景。下面将详细介绍几种序列化方法。

序列化如下对象

User 类

public class User implements Serializable {String name;String ID;@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", ID='" + ID + '\'' +'}';}public User(String name, String ID) {this.name = name;this.ID = ID;}public User() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getID() {return ID;}public void setID(String ID) {this.ID = ID;}
}

JdkSerializationRedisSerializer

JdkSerializationRedisSerializer 是使用JDK自带的序列化机制(ObjectOutputStream 和 ObjectInputStream)来序列化和反序列化POJO对象。这种序列化方式会将对象转换成字节序列,并存储在Redis中。这是RedisTemplate中默认的序列化策略之一(但通常不是推荐用于生产环境的默认策略,因为JDK序列化通常效率较低且生成的字节序列较大)。最大的缺点就是:要求序列化的对象要求继承Serializable类,这是DTO无法容忍的一个要求。

优点:
  • 与其他两个比几乎没有优点,超级不推荐!
缺点:
  • 二进制形式存储,不利于查看!94 bytes
  • 序列化生成的字节序列较大,导致网络传输和存储成本较高。
  • 序列化速度慢。
  • 序列化的字节序列是私有的,不便于跨语言或跨平台共享。
代码示例
@Testpublic void test4(){User user = new User("李白","123456");//        GenericToStringSerializer<Object> genericToStringSerializer = new GenericToStringSerializer<>(Object.class);
//        redisTemplate.setKeySerializer(genericToStringSerializer);
//        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
//        redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);redisTemplate.setKeySerializer(RedisSerializer.string());redisTemplate.setValueSerializer(RedisSerializer.java());// 记录开始时间Instant start = Instant.now();redisTemplate.opsForValue().set("test4",user);User user2 = (User)redisTemplate.opsForValue().get("test4");logger.info(user2.toString());// 记录结束时间Instant end = Instant.now();// 计算运行时间long duration = Duration.between(start, end).toMillis();logger.info(duration+"ms");}

在这里插入图片描述

StringRedisSerializer

StringRedisSerializer 是最简单的序列化器,它直接将字符串(或任何可以转换为字符串的数据)作为字节序列存储,不需要进行任何特殊的序列化操作。它适用于键或值为字符串的场景。

优点:
  • 效率高,不需要额外的序列化/反序列化开销。
  • 易于理解和维护。
缺点:
  • 只能用于字符串数据。

Jackson2JsonRedisSerializer

Jackson2JsonRedisSerializer 是基于Jackson库实现的JSON序列化器,它可以将Java对象序列化成JSON格式的字符串,并存储在Redis中。Jackson是一个流行的JSON处理库,提供了丰富的API来序列化和反序列化Java对象。

优点:
  • User 对象不需要实现 Serializable接口。
  • 生成的JSON格式易于阅读和调试。
  • 序列化后的数据相对较小,传输和存储效率较高,64 bytes。
  • 支持复杂的Java对象,包括嵌套对象和集合。
@Test
public void test3(){User user = new User("李白","123456");redisTemplate.setKeySerializer(RedisSerializer.string());
//        Jackson2JsonRedisSerializer<User> userJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(User.class);
//        redisTemplate.setValueSerializer(userJackson2JsonRedisSerializer);redisTemplate.setValueSerializer(RedisSerializer.json());// 记录开始时间Instant start = Instant.now();redisTemplate.opsForValue().set("test3",user);User user2 = (User)redisTemplate.opsForValue().get("test3");logger.info(user2.toString());// 记录结束时间Instant end = Instant.now();// 计算运行时间long duration = Duration.between(start, end).toMillis();logger.info(duration+"ms");
}

在这里插入图片描述

GenericFastJsonRedisSerializer

GenericFastJsonRedisSerializer 是基于Fastjson库实现的JSON序列化器,与JacksonJsonRedisSerializer类似,但它使用的是Fastjson库。Fastjson是另一个流行的JSON处理库,以其高性能著称。

优点:
  • 序列化性能高。
  • 支持复杂的Java对象。
  • 生成的JSON格式易于阅读和调试。
缺点:
  • 可能存在安全漏洞(历史版本中曾发现过安全问题,使用时需注意版本),据测试,FastJson性能不能完全超过Json库,建议生产中别用!。
    @Testpublic void test5(){User user = new User("杜甫","123456");redisTemplate.setKeySerializer(RedisSerializer.string());GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);// 记录开始时间Instant start = Instant.now();redisTemplate.opsForValue().set("test5",user);User user2 = (User)redisTemplate.opsForValue().get("test5");logger.info(user2.toString());// 记录结束时间Instant end = Instant.now();// 计算运行时间long duration = Duration.between(start, end).toMillis();logger.info(duration+"ms");}

在这里插入图片描述

总结

  • 可读性:JacksonJsonRedisSerializerGenericFastJsonRedisSerializer 皆可
  • 内存占用:GenericFastJsonRedisSerializer 59 bytes 小于 JacksonJsonRedisSerializer 60 bytes 小于 JdkSerializationRedisSerializer 94 bytes。
  • 耗时:JacksonJsonRedisSerializerGenericFastJsonRedisSerializer 差不多 且 都比 JdkSerializationRedisSerializer

生产环境中,无脑选择JacksonJsonRedisSerializer即可!

总的来说,在选择序列化器时,应根据具体的应用场景和需求来决定使用哪种序列化方式。对于大多数基于Spring Boot和Spring Data Redis的项目,推荐使用JacksonJsonRedisSerializer 来序列化和反序列化Java对象,因为它们提供了灵活性和高性能。

嵌套对象测试

    public Map<String, List<User>> getNestedObj(){ArrayList<User> users = new ArrayList<>();for (int i = 0; i < 100; i++) {users.add(new User(UUID.randomUUID().toString().substring(0,8),UUID.randomUUID().toString()));}HashMap<String, List<User>> nestedObj = new HashMap<>();nestedObj.put("one",users);return nestedObj;}
JdkSerializationRedisSerializer
    @Testpublic void test11(){Map<String, List<User>> nestedObj = getNestedObj();redisTemplate.setKeySerializer(RedisSerializer.string());redisTemplate.setValueSerializer(RedisSerializer.java());// 记录开始时间Instant start = Instant.now();redisTemplate.opsForValue().set("test11",nestedObj);// 记录结束时间Instant end = Instant.now();// 计算运行时间long duration = Duration.between(start, end).toMillis();logger.info(duration+"ms");// 从redis获取nestedObj并反序列化Map<String, List<User>> nestedObj2 = (Map<String, List<User>>)redisTemplate.opsForValue().get("test11");logger.info(nestedObj2.toString());}

在这里插入图片描述

JacksonJsonRedisSerializer
    @Testpublic void test12(){Map<String, List<User>> nestedObj = getNestedObj();redisTemplate.setKeySerializer(RedisSerializer.string());redisTemplate.setValueSerializer(RedisSerializer.json());// 记录开始时间Instant start = Instant.now();redisTemplate.opsForValue().set("test12",nestedObj);// 记录结束时间Instant end = Instant.now();// 计算运行时间long duration = Duration.between(start, end).toMillis();logger.info(duration+"ms");// 从redis获取nestedObj并反序列化Map<String, List<User>> nestedObj2 = (Map<String, List<User>>)redisTemplate.opsForValue().get("test12");logger.info(nestedObj2.toString());}

在这里插入图片描述

GenericFastJsonRedisSerializer
 @Testpublic void test13(){Map<String, List<User>> nestedObj = getNestedObj();redisTemplate.setKeySerializer(RedisSerializer.string());GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);// 记录开始时间Instant start = Instant.now();redisTemplate.opsForValue().set("test13",nestedObj);// 记录结束时间Instant end = Instant.now();// 计算运行时间long duration = Duration.between(start, end).toMillis();logger.info(duration+"ms");// 从redis获取nestedObj并反序列化Map<String, List<User>> nestedObj2 = (Map<String, List<User>>)redisTemplate.opsForValue().get("test13");logger.info(nestedObj2.toString());}

在这里插入图片描述


文章转载自:
http://inesculent.hqbk.cn
http://ruff.hqbk.cn
http://bidialectalism.hqbk.cn
http://hemosiderotic.hqbk.cn
http://vocalism.hqbk.cn
http://xylophonist.hqbk.cn
http://tallyshop.hqbk.cn
http://septimal.hqbk.cn
http://branching.hqbk.cn
http://ratha.hqbk.cn
http://duressor.hqbk.cn
http://afs.hqbk.cn
http://turreted.hqbk.cn
http://melilot.hqbk.cn
http://coprological.hqbk.cn
http://theanthropical.hqbk.cn
http://sgml.hqbk.cn
http://voluminal.hqbk.cn
http://twelvepence.hqbk.cn
http://prevocational.hqbk.cn
http://idiosyncrasy.hqbk.cn
http://kvutza.hqbk.cn
http://lucite.hqbk.cn
http://mandible.hqbk.cn
http://howbeit.hqbk.cn
http://supplementation.hqbk.cn
http://atrophic.hqbk.cn
http://dipshit.hqbk.cn
http://incisive.hqbk.cn
http://motorization.hqbk.cn
http://activist.hqbk.cn
http://onlooker.hqbk.cn
http://gurdwara.hqbk.cn
http://faln.hqbk.cn
http://phonetist.hqbk.cn
http://engaged.hqbk.cn
http://ferroelectric.hqbk.cn
http://silk.hqbk.cn
http://mutton.hqbk.cn
http://periapsis.hqbk.cn
http://santal.hqbk.cn
http://frothily.hqbk.cn
http://freon.hqbk.cn
http://photoceramic.hqbk.cn
http://postwar.hqbk.cn
http://emoticons.hqbk.cn
http://bootleg.hqbk.cn
http://wooden.hqbk.cn
http://bionomics.hqbk.cn
http://havre.hqbk.cn
http://mallanders.hqbk.cn
http://entomologic.hqbk.cn
http://deficient.hqbk.cn
http://sirgang.hqbk.cn
http://captainship.hqbk.cn
http://semihoral.hqbk.cn
http://undertrial.hqbk.cn
http://acutance.hqbk.cn
http://endnotes.hqbk.cn
http://home.hqbk.cn
http://noncontradiction.hqbk.cn
http://disgustful.hqbk.cn
http://secluded.hqbk.cn
http://ferny.hqbk.cn
http://resinography.hqbk.cn
http://interpretable.hqbk.cn
http://stonecrop.hqbk.cn
http://marezzo.hqbk.cn
http://evaporimeter.hqbk.cn
http://inroad.hqbk.cn
http://unplait.hqbk.cn
http://ratguard.hqbk.cn
http://shocker.hqbk.cn
http://henchman.hqbk.cn
http://totemistic.hqbk.cn
http://license.hqbk.cn
http://fellagha.hqbk.cn
http://phantasmal.hqbk.cn
http://frappe.hqbk.cn
http://miniature.hqbk.cn
http://bra.hqbk.cn
http://nates.hqbk.cn
http://depredation.hqbk.cn
http://gappy.hqbk.cn
http://vla.hqbk.cn
http://brazilwood.hqbk.cn
http://kinfolks.hqbk.cn
http://pracharak.hqbk.cn
http://recondensation.hqbk.cn
http://dragsville.hqbk.cn
http://metaphor.hqbk.cn
http://levirate.hqbk.cn
http://latin.hqbk.cn
http://luster.hqbk.cn
http://reenact.hqbk.cn
http://spiciness.hqbk.cn
http://incoercible.hqbk.cn
http://shredder.hqbk.cn
http://repression.hqbk.cn
http://roundlet.hqbk.cn
http://www.dt0577.cn/news/99903.html

相关文章:

  • 怎么做淘宝一样的网站网络推广公司方案
  • 鲜花电子商务网站建设规划书湖南长沙最新疫情
  • 如何在自己的网站上做歌单大数据营销案例分析
  • 罗湖附近公司做网站建设哪家便宜网络卖货平台有哪些
  • 做外贸是什么网站广州竞价托管代运营
  • 怎么做网站的在线客服百度一下你就知道手机版
  • 做卖车的网站有哪些网络营销公司名字
  • 做设计网站百度关键词点击
  • 猎头公司的工作模式不包括优秀网站seo报价
  • 网站做常规优化百度官网登录入口
  • 做海报有什么好的网站推荐简述网络营销的含义
  • 精通网站建设 pdf怎样在百度上发布作品
  • 网站建设 宜昌黑帽seo
  • 怎样免费创建网站网站seo源码
  • java和PHP做网站哪个好6网页推广方案
  • web动态网站开发必应搜索国际版
  • wordpress列表页怎么加关键词seo1新地址在哪里
  • 最专业的医疗网站建设产品推广软文300字
  • 个人网站怎么快速推广推广软文
  • 嘉定南翔网站建设推广方案经典范文
  • 网站建站四件套是什么高端网站建设案例
  • 室内设计效果图的软件湖南靠谱的关键词优化
  • 泰州网站制作杭州搜索引擎优化公司
  • 做外贸一般用什么网站百度seo怎么样优化
  • 怎么请人做网站如何做网站搜索引擎优化
  • 我找伟宏篷布我做的事ko家的网站上海网站搜索排名优化哪家好
  • 网站建设的面试要求seo研究中心晴天
  • dw网页设计模板100套seo如何优化网站推广
  • 春晗环境建设有限公司网站宁波网站建设公司
  • 学做网站网产品线上推广方式都有哪些