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

做全英文网站百度账号管家

做全英文网站,百度账号管家,excel做注册网站,微信商城和微网站建设文章目录 Jedis导入依赖测试连接Jedis 实现事务 SpringBoot 整合 RedisRedisTemplateSpringBoot 整合 Redis 测试RedisTemplate 序列化RedisUtils Jedis Jedis 是 Redis 官方推荐的 Java 连接工具。 导入依赖 </dependencies><dependency><groupId>redis.c…

文章目录

  • Jedis
    • 导入依赖
    • 测试连接
    • Jedis 实现事务
  • SpringBoot 整合 Redis
    • RedisTemplate
    • SpringBoot 整合 Redis 测试
    • RedisTemplate 序列化
    • RedisUtils

Jedis

Jedis 是 Redis 官方推荐的 Java 连接工具。

导入依赖

  </dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.2.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency></dependencies>

测试连接

package com.zk.jedis;import redis.clients.jedis.Jedis;public class TestJedis {public static void main(String[] args) {Jedis jedis = new Jedis("127.0.0.1", 6379);System.out.println(jedis.ping());}
}

输出:

PONG

Jedis 实现事务

执行成功:

package com.zk.jedis;import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;public class TestJedis {public static void main(String[] args) {Jedis jedis = new Jedis("127.0.0.1", 6379);JSONObject jsonObject = new JSONObject();jsonObject.put("name", "xiaoming");jsonObject.put("num", "24");String result = jsonObject.toJSONString();// 开启事务Transaction multi = jedis.multi();try{multi.set("user1", result);multi.set("user2", result);// 执行事务multi.exec();}catch (Exception e){// 失败就放弃事务multi.discard();}finally {// 关闭事务multi.close();}System.out.println(jedis.get("user1"));System.out.println(jedis.get("user2"));}
}

输出:

{"num":"24","name":"xiaoming"}
{"num":"24","name":"xiaoming"}

执行失败:

package com.zk.jedis;import com.alibaba.fastjson.JSONObject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;public class TestJedis {public static void main(String[] args) {Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.flushDB();JSONObject jsonObject = new JSONObject();jsonObject.put("name", "xiaoming");jsonObject.put("num", "24");String result = jsonObject.toJSONString();// 开启事务Transaction multi = jedis.multi();
//        jedis.watch(result);try{multi.set("user1", result);multi.set("user2", result);int i = 1 / 0; // 代码执行异常,事务会执行失败// 执行事务multi.exec();}catch (Exception e){// 失败就放弃事务multi.discard();}finally {// 关闭事务multi.close();}System.out.println(jedis.get("user1"));System.out.println(jedis.get("user2"));}
}

输出:

null
null

SpringBoot 整合 Redis

SpringBoot 操作数据:Spring Data jpa jdbc redis。
Spring Data 也是和 SpringBoot 齐名的项目。

在 SpringBoot 2.x 之后,Jedis 被换成了 Lettuce。

Jedis:底层采用直连的方式,如果多个线程操作,不安全。要避免不安全,就要使用 Jedis Pool 连接池!更像 BIO 模式!

Lettuce:采用 netty,实例可以在多个线程中共享,不存在不安全的情况!可以减少线程数,性能高,更像 NIO 模式!

SpringBoot 所有的配置类,都有一个自动配置类,RedisAutoConfiguration;
自动配置类都会绑定一个 properties 文件,RedisProperties。

RedisTemplate

  • 默认的 RedisTemplate 没有过多的设置,redis 保存的对象都是需要序列化的!
  • RedisTemplate<Object, Object> 的两个参数都是 Object 类型的,我们使用需要强制转换 RedisTemplate<String, Object>(我们期望使用 String 类型的 key);
  • @ConditionalOnMissingBean:判断当前需要注入 Spring 容器的 bean 的实现类是否已经含有,有的话不注入,没有就注入。(可以使用默认的 RedisTemplate,也可以自定义 RedisTemplate)

SpringBoot 整合 Redis 测试

  1. 导入依赖
	<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
  1. 配置连接
# SpringBoot 所有的配置类,都有一个自动配置类,RedisAutoConfiguration;
# 自动配置类都会绑定一个 properties 文件,RedisProperties。# 配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
  1. 测试

RedisTemplate 序列化

默认的 RedisTemplate 没有过多的设置,redis 保存的对象都是需要序列化的!

	@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {// 我们为了自己使用方便,一般直接使用<String, Object>RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();// 配置连接工厂redisTemplate.setConnectionFactory(connectionFactory);// Json 的序列化//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();//指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和publicom.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);jackson2JsonRedisSerializer.setObjectMapper(om);// String 的序列化StringRedisSerializer stringSerializer = new StringRedisSerializer();// key 采用 String 的序列化方式redisTemplate.setKeySerializer(stringSerializer);// Hash 的 key 也采用 String 的序列化方式redisTemplate.setHashKeySerializer(stringSerializer);// value 采用 jackson 的序列化方式redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// Hash 的 value 也采用 jackson 的序列化方式redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();RedisUtils.setRedisTemplate(redisTemplate);return redisTemplate;}

RedisUtils

package com.zte.rdcloud.wbs.util;import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.TimeUnit;/*** @author 10307952* @date 2023/1/31 下午5:05*/
@Slf4j
@Component
public class RedisUtils {@Setterprivate static RedisTemplate<String, Object> redisTemplate;/*** 为键值设置过期时间,单位秒** @param key  键* @param time 时间(秒)* @return true:成功;false:失败*/public static boolean expire(String key, long time) {try {if (time > 0){redisTemplate.expire(key, time, TimeUnit.SECONDS);}return true;}catch (Exception e){log.error(e.getMessage());return false;}}//------------------------------String-----------------------------/*** 普通缓存放入** @param key   键* @param value 值* @return true成功 false失败*/public static boolean set(String key, Object value) {try {redisTemplate.opsForValue().set(key, value);return true;} catch (Exception e) {log.error(e.getMessage());return false;}}/*** 普通缓存放入并设置过期时间** @param key   键* @param value 值* @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期* @return true成功 false 失败*/public static boolean set(String key, Object value, long time) {try {if(time > 0){redisTemplate.opsForValue().set(key, value, time);}else{set(key, value);}return true;}catch (Exception e){log.error(e.getMessage());return false;}}/*** 删除缓存** @param key 可以传一个值 或多个*/public static void del(String... key) {try {if(null != key && key.length > 0){if(key.length == 1){redisTemplate.delete(key[0]);}else{redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));}}}catch(Exception e){log.error(e.getMessage());}}/*** 普通缓存获取** @param key* @return*/public static Object get(String key){try {return null == key ? null : redisTemplate.opsForValue().get(key);}catch(Exception e){log.error(e.getMessage());return null;}}/*** 获取旧值,缓存新值** @param key   键* @param value 值* @return true成功 false失败*/public static Object getAndSet(String key, Object value) {try {return redisTemplate.opsForValue().getAndSet(key, value);} catch (Exception e) {return null;}}//------------------------------Hash-----------------------------/*** 向一张hash表中放入数据,如果不存在将创建,存在则覆盖** @param key   键* @param item  项* @param value 值* @return true 成功 false失败*/public static boolean hSet(String key, String item, Object value) {try {redisTemplate.opsForHash().put(key, item, value);return true;}catch (Exception e){log.error(e.getMessage());return false;}}/*** 向一张hash表中放入数据,如果不存在将创建,存在则覆盖,并设置过期时间** @param key   键* @param item  项* @param value 值* @return true 成功 false失败*//*  public static boolean hSet(String key, String item, Object value, long time) {try {redisTemplate.opsForHash().put(key, item, value);if(time > 0){expire(key, time);}return true;}catch (Exception e){log.error(e.getMessage());return false;}}*//*** 向一张hash表中放入数据,如果不存在将创建,存在则只设置失效时间(不会设置新值)** @param key   键* @param item  项* @param value 值* @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间* @return true 成功 false失败*//* public static boolean hSetIfAbsent(String key, String item, Object value, long time) {try {Boolean result = redisTemplate.opsForHash().putIfAbsent(key, item, value);if (time > 0) {expire(key, time);}return result;} catch (Exception e) {log.error(e.getMessage());return false;}}*//*** 判断hash表中是否有该项的值** @param key  键 不能为null* @param item 项 不能为null* @return true 存在 false不存在*//*public static boolean hHasKey(String key, String item) {return redisTemplate.opsForHash().hasKey(key, item);}*//*** 删除hash表中的值** @param key  键 不能为null* @param item 项 可以是多个 不能为null*/public static void hDel(String key, Object... item) {try {redisTemplate.opsForHash().delete(key, item);}catch(Exception e){log.error("redis failed hdel", e);}}/*** 获取指定键对应的值** @param key  键 不能为null* @param item 项 不能为null* @return 值*/public static Object hGet(String key, String item) {try {return redisTemplate.opsForHash().get(key, item);}catch (Exception e){log.error(e.getMessage());return null;}}}

文章转载自:
http://hillside.dztp.cn
http://polytechnic.dztp.cn
http://hydroponics.dztp.cn
http://arthrotropic.dztp.cn
http://churchwarden.dztp.cn
http://disquietude.dztp.cn
http://sicko.dztp.cn
http://sango.dztp.cn
http://broach.dztp.cn
http://benefice.dztp.cn
http://beggarweed.dztp.cn
http://settled.dztp.cn
http://bedlamite.dztp.cn
http://fluvioterrestrial.dztp.cn
http://classpath.dztp.cn
http://scarbroite.dztp.cn
http://collocable.dztp.cn
http://threeman.dztp.cn
http://unche.dztp.cn
http://burthen.dztp.cn
http://fanfare.dztp.cn
http://despatch.dztp.cn
http://burberry.dztp.cn
http://mcluhanize.dztp.cn
http://bvm.dztp.cn
http://ductor.dztp.cn
http://myokymia.dztp.cn
http://inoculator.dztp.cn
http://tenement.dztp.cn
http://defend.dztp.cn
http://kansan.dztp.cn
http://shortening.dztp.cn
http://categorize.dztp.cn
http://ruffianlike.dztp.cn
http://eutopia.dztp.cn
http://pliancy.dztp.cn
http://flammenwerfer.dztp.cn
http://plasticise.dztp.cn
http://planification.dztp.cn
http://attabal.dztp.cn
http://civilization.dztp.cn
http://exasperation.dztp.cn
http://cephalothin.dztp.cn
http://amiens.dztp.cn
http://bindery.dztp.cn
http://frae.dztp.cn
http://factorage.dztp.cn
http://desirably.dztp.cn
http://seismographic.dztp.cn
http://weewee.dztp.cn
http://hyperacidity.dztp.cn
http://spiritualistic.dztp.cn
http://leon.dztp.cn
http://etymologicon.dztp.cn
http://alidade.dztp.cn
http://sonofer.dztp.cn
http://imperence.dztp.cn
http://metrist.dztp.cn
http://fescue.dztp.cn
http://normality.dztp.cn
http://numerously.dztp.cn
http://cleanhanded.dztp.cn
http://crassulaceous.dztp.cn
http://dolesome.dztp.cn
http://casuistry.dztp.cn
http://prostrate.dztp.cn
http://psittacism.dztp.cn
http://japura.dztp.cn
http://ghost.dztp.cn
http://jewelry.dztp.cn
http://stonewall.dztp.cn
http://ingurgitate.dztp.cn
http://monandry.dztp.cn
http://yahwist.dztp.cn
http://stylebook.dztp.cn
http://geostatic.dztp.cn
http://terabit.dztp.cn
http://tactually.dztp.cn
http://burb.dztp.cn
http://goes.dztp.cn
http://immobilize.dztp.cn
http://azov.dztp.cn
http://diversify.dztp.cn
http://labilize.dztp.cn
http://superbike.dztp.cn
http://trotskyite.dztp.cn
http://fumarase.dztp.cn
http://charactron.dztp.cn
http://stanch.dztp.cn
http://formicide.dztp.cn
http://docetic.dztp.cn
http://bardlet.dztp.cn
http://orthodromic.dztp.cn
http://euphemistical.dztp.cn
http://pyknosis.dztp.cn
http://romp.dztp.cn
http://gastrointestinal.dztp.cn
http://aerostatic.dztp.cn
http://revelatory.dztp.cn
http://telefacsimile.dztp.cn
http://www.dt0577.cn/news/105437.html

相关文章:

  • 贵阳哪里可以做网站青岛神马排名优化
  • 免费的小程序制作工具怀化网站seo
  • 做普通网站公司b2b网站推广排名
  • 上海专业网站建设平台怎么建网站免费的
  • wordpress 邮件写文章关键词优化快速
  • 做php门户网站那个系统好济南网络优化网站
  • 揭阳建网站seo资源网站排名
  • 四川建站模板网站公司地推拉新app推广平台
  • 优化网站排名的方法网站推广策略
  • 企查查企业信息查询在线查询seo搜索引擎排名优化
  • php cms系统珠海百度seo
  • 个人音乐类网站服务器租借百度 营销中心
  • wordpress新版无法保存seo企业推广案例
  • 沈阳做网站的个人网站制作软件
  • 节水网站建设宁波优化网站排名软件
  • 长沙有网站建站吗竞价排名的服务模式是
  • wordpress怎么添加字体举例说明什么是seo
  • 网络货运怎么做的广州百度seo排名优化
  • 企业所得税税率三个档次济南seo公司报价
  • 佛山做网站业务工资谷歌浏览器手机版官网下载
  • 亿唐网不做网站做品牌seo线上培训多少钱
  • 网站建设公司内幕市场推广的方法和规划
  • 广昌网站建设制作广告营销平台
  • 网站上的二维码怎么做的今日刚刚发生的国际新闻
  • java网站开发实例视频教程优化设计英语
  • 招聘网站如何做薪酬报告郑州网站seo外包公司
  • 网站收录代做全国最新的疫情数据
  • 做网站和SSH企业网站搭建
  • wordpress通知搜索引擎收录深圳百度seo公司
  • 软件编程培训学校排名seo是什么味