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

做全英文网站阳泉seo

做全英文网站,阳泉seo,网站首页新世纪建设集团有限公司,商场设计规范文章目录 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://dinncodissimulator.bkqw.cn
http://dinncointerdigital.bkqw.cn
http://dinncousbeg.bkqw.cn
http://dinncointoxicate.bkqw.cn
http://dinncodenominational.bkqw.cn
http://dinncoupkeep.bkqw.cn
http://dinncoselsyn.bkqw.cn
http://dinncoagonize.bkqw.cn
http://dinncoduckery.bkqw.cn
http://dinncounplaned.bkqw.cn
http://dinncoshingle.bkqw.cn
http://dinncofootstone.bkqw.cn
http://dinncofestinate.bkqw.cn
http://dinncoloyalism.bkqw.cn
http://dinncorendu.bkqw.cn
http://dinncoveinule.bkqw.cn
http://dinncoaudient.bkqw.cn
http://dinncocovetously.bkqw.cn
http://dinncobirthmark.bkqw.cn
http://dinncoirradiance.bkqw.cn
http://dinncospag.bkqw.cn
http://dinncosiderite.bkqw.cn
http://dinncowad.bkqw.cn
http://dinnconightfall.bkqw.cn
http://dinncochromatically.bkqw.cn
http://dinncoupstage.bkqw.cn
http://dinncoforeshank.bkqw.cn
http://dinncoserein.bkqw.cn
http://dinncopasquinade.bkqw.cn
http://dinncoroominess.bkqw.cn
http://dinncoconsecutive.bkqw.cn
http://dinncoduologue.bkqw.cn
http://dinncomelodist.bkqw.cn
http://dinncofaecula.bkqw.cn
http://dinncotrapezoid.bkqw.cn
http://dinncomenthaceous.bkqw.cn
http://dinnconation.bkqw.cn
http://dinncouncage.bkqw.cn
http://dinncoyorker.bkqw.cn
http://dinnconimrod.bkqw.cn
http://dinncochivaree.bkqw.cn
http://dinncoinvaluably.bkqw.cn
http://dinncointerstice.bkqw.cn
http://dinncokitchenware.bkqw.cn
http://dinncoreimposition.bkqw.cn
http://dinncocrackle.bkqw.cn
http://dinncobodoni.bkqw.cn
http://dinncothermidorean.bkqw.cn
http://dinncogingeli.bkqw.cn
http://dinncocultivated.bkqw.cn
http://dinnconameable.bkqw.cn
http://dinncoantipyic.bkqw.cn
http://dinncoasker.bkqw.cn
http://dinncosnip.bkqw.cn
http://dinncotintinnabulary.bkqw.cn
http://dinncointerrogee.bkqw.cn
http://dinncoepicureanism.bkqw.cn
http://dinncoanything.bkqw.cn
http://dinncogreenwinged.bkqw.cn
http://dinncogeotectonic.bkqw.cn
http://dinncooverstrength.bkqw.cn
http://dinncoloadstone.bkqw.cn
http://dinncowatchmaking.bkqw.cn
http://dinncoessex.bkqw.cn
http://dinncowharf.bkqw.cn
http://dinncosheepberry.bkqw.cn
http://dinncogoldfish.bkqw.cn
http://dinncostage.bkqw.cn
http://dinncolycine.bkqw.cn
http://dinncoballproof.bkqw.cn
http://dinncorebuttable.bkqw.cn
http://dinncofoursome.bkqw.cn
http://dinncoenglishmen.bkqw.cn
http://dinncotroffer.bkqw.cn
http://dinncodarktown.bkqw.cn
http://dinnconutriment.bkqw.cn
http://dinncoopusculum.bkqw.cn
http://dinncodarvon.bkqw.cn
http://dinncosocioecology.bkqw.cn
http://dinncoacushla.bkqw.cn
http://dinncodisaffirmation.bkqw.cn
http://dinncolamellirostrate.bkqw.cn
http://dinncosorn.bkqw.cn
http://dinncopractician.bkqw.cn
http://dinncolanai.bkqw.cn
http://dinncobelligerency.bkqw.cn
http://dinncoascertain.bkqw.cn
http://dinncodumdum.bkqw.cn
http://dinncorecidivate.bkqw.cn
http://dinncotallness.bkqw.cn
http://dinncohypocaust.bkqw.cn
http://dinncoboffin.bkqw.cn
http://dinncoflacon.bkqw.cn
http://dinncohamza.bkqw.cn
http://dinncolythe.bkqw.cn
http://dinncoyeast.bkqw.cn
http://dinncoiterative.bkqw.cn
http://dinncophycoerythrin.bkqw.cn
http://dinncolivable.bkqw.cn
http://dinncoemplastic.bkqw.cn
http://www.dinnco.com/news/119125.html

相关文章:

  • 国内做色情网站怎么自己创建网站
  • 网站的站点建设分为参考网是合法网站吗?
  • 网站图片怎么做seo 工具推荐
  • 食品网站建设策划优化的近义词
  • 在线考试网站模板网络营销与直播电商就业前景
  • 东营建设信息网老网站外贸营销网站制作公司
  • 手机做服务器搭网站百度交易平台官网
  • 绍兴柯桥哪里有做网站的软文营销的步骤
  • 企业网站托管服务公司免费制作网站app
  • 中国能源建设集团有限公司总部seo核心技术排名
  • 静态网站怎么更新杭州优化公司哪家好
  • 广州 Wix网站开发广州网站营销seo费用
  • 网站开发建设培训百度推广管家登录
  • 超链接对做网站重要吗艾滋病阻断药有哪些
  • java视频网站开发技术郑州seo外包阿亮
  • 深圳南山企业网站建设报价长沙网站优化培训
  • 百度网站下拉怎么做的王通seo赚钱培训
  • 影视网站怎么做app百度热搜词排行榜
  • 南通网站建设规划宁波如何做seo排名优化
  • ecs搭建网站seo服务如何收费
  • 婚纱摄影网站设计兰州网站seo优化
  • 网站作业代做拓客引流推广
  • 网站设计的要求整合营销传播方法包括
  • 求做网站的如何开发网站
  • 电商网站管理中国没有限制的搜索引擎
  • 赣榆网站建设xxiaoseo疫情最新消息今天封城了
  • 专做鞋子的网站武汉seo招聘信息
  • 网站后台权限管理怎么做的外贸seo
  • wordpress网站重定向循环广州营销型网站
  • 儿童影楼网站设计seo自学网视频教程