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

个人摄影网站模板国际电商怎么做

个人摄影网站模板,国际电商怎么做,西安企业网站建设高新区,vr软件开发需要学什么#x1f497;wei_shuo的个人主页 #x1f4ab;wei_shuo的学习社区 #x1f310;Hello World #xff01; Redis事务操作 Redis事务是一组命令的集合#xff0c;这些命令会作为一个整体被执行#xff0c;要么全部执行成功#xff0c;要么全部执行失败#xff1b;Redis事… wei_shuo的个人主页 wei_shuo的学习社区 Hello World Redis事务操作 Redis事务是一组命令的集合这些命令会作为一个整体被执行要么全部执行成功要么全部执行失败Redis事务通过MULTI、EXEC、DISCARD和WATCH四个命令来实现 MULTI开启一个新的事务 EXEC提交事务 DISCARD取消事务 WATCH监控一个或多个键当这些键被其他客户端修改时当前事务会被中断 Redis乐观锁 乐观锁是一种并发控制机制它假设冲突的概率比较小因此不会在访问共享资源时加锁而是在更新时检查数据是否被其他进程修改过 Redis悲观锁 悲观锁是指在对共享资源进行操作时认为其他线程或进程会对该资源进行修改因此在操作前先加锁以防止其他线程或进程的干扰 Jedis Jedis是一个Java语言编写的Redis客户端库用于在Java应用程序中连接和操作Redis数据库Jedis库是开源的可以通过Maven或Gradle等构建工具进行引入和使用 dependencygroupIdcom.github.luues.boot/groupIdartifactIdspring-boot-starter-jedis/artifactIdversion1.0.3.5.RELEASE/version /dependencySpringboot集成Redis 依赖导入 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencyapplication.properties #配置Redis spring.redis.host127.0.0.1 spring.redis.port6379测试 package com.wei;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.RedisConnection; import org.springframework.data.redis.core.RedisTemplate;SpringBootTest class SpringbootRedisApplicationTests {Autowired(required false)private RedisTemplate redisTemplate;Testvoid contextLoads() {/*** RedisTemplate 操作不同是数据类型* opsForValue 操作String* opsForList 操作List* opsForHash 操作Hash* ……*//*** 操作数据库连接* redisTemplate.getConnectionFactory().getConnection();*/RedisConnection connection redisTemplate.getConnectionFactory().getConnection();/*connection.flushDb(); 用于刷新查询缓存和结果集合以提高查询性能connection.flushAll(); 用于刷新整个数据库包括所有的表、视图、存储过程和触发器等*/connection.flushDb();connection.flushAll();}}package com.wei;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.RedisConnection; import org.springframework.data.redis.core.RedisTemplate;SpringBootTest class SpringbootRedisApplicationTests {Autowired(required false)private RedisTemplate redisTemplate;Testvoid contextLoads() {/*** RedisTemplate 操作不同是数据类型* opsForValue 操作String* opsForList 操作List* opsForHash 操作Hash* ……*/redisTemplate.opsForValue().set(mykey,weishuo);System.out.println(redisTemplate.opsForValue().get(mykey));}}源码 RedisAutoConfiguration.java package org.springframework.boot.autoconfigure.data.redis;import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;AutoConfiguration ConditionalOnClass({RedisOperations.class}) EnableConfigurationProperties({RedisProperties.class}) Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) public class RedisAutoConfiguration {public RedisAutoConfiguration() {}BeanConditionalOnMissingBean(name {redisTemplate})ConditionalOnSingleCandidate(RedisConnectionFactory.class)public RedisTemplateObject, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplateObject, Object template new RedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}BeanConditionalOnMissingBeanConditionalOnSingleCandidate(RedisConnectionFactory.class)public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {return new StringRedisTemplate(redisConnectionFactory);} }RedisProperties.java public class RedisProperties {private int database 0;private String url;private String host localhost;private String username;private String password;private int port 6379;private boolean ssl;private Duration timeout;private Duration connectTimeout;private String clientName;private RedisProperties.ClientType clientType;private RedisProperties.Sentinel sentinel;private RedisProperties.Cluster cluster;private final RedisProperties.Jedis jedis new RedisProperties.Jedis();private final RedisProperties.Lettuce lettuce new RedisProperties.Lettuce();…… }自定义配置类 config/RedisConfig.java package com.wei.config;import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; 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.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer;import java.net.UnknownHostException;Configuration SuppressWarnings(all) //镇压所有警告 public class RedisConfig {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory redisConnectionFactory)throws UnknownHostException { // 默认的连接配置和源码保持一致即可RedisTemplateString, Object template new RedisTemplate();template.setConnectionFactory(redisConnectionFactory);// 序列化配置 // json序列化配置--JacksonJackson2JsonRedisSerializerObject objectJackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);objectJackson2JsonRedisSerializer.setObjectMapper(objectMapper);// string的序列化StringRedisSerializer stringRedisSerializer new StringRedisSerializer();// string的key和hash的key都采用string的序列化 // value都采用Jackson的序列化//key采用string序列化方式template.setKeySerializer(stringRedisSerializer);//hash的key采用string序列化方式template.setHashKeySerializer(stringRedisSerializer);//value采用Jackson序列化方式template.setValueSerializer(objectJackson2JsonRedisSerializer);//hash的value采用Jackson序列化方式template.setHashValueSerializer(objectJackson2JsonRedisSerializer);return template;} }Redis工具类封装 utils/RedisUtil.java package com.wei.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils;import java.util.*; import java.util.concurrent.TimeUnit; Component public final class RedisUtil {Autowiredprivate RedisTemplateString, Object redisTemplate; // common/*** 指定缓存失效时间* param key 键* param time 时间(秒)*/public boolean expire(String key, long time) {try {if (time 0) {redisTemplate.expire(key, time, TimeUnit.SECONDS);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据key 获取过期时间* param key 键 不能为null* return 时间(秒) 返回0代表为永久有效*/public long getExpire(String key) {return redisTemplate.getExpire(key, TimeUnit.SECONDS);}/*** 判断key是否存在* param key 键* return true 存在 false不存在*/public boolean hasKey(String key) {try {return redisTemplate.hasKey(key);} catch (Exception e) {e.printStackTrace();return false;}}/*** 删除缓存* param key 可以传一个值 或多个*/SuppressWarnings(unchecked)public void del(String... key) {if (key ! null key.length 0) {if (key.length 1) {redisTemplate.delete(key[0]);} else {redisTemplate.delete((CollectionString) CollectionUtils.arrayToList(key));}}} // String/*** 普通缓存获取* param key 键* return 值*/public Object get(String key) {return key null ? null : redisTemplate.opsForValue().get(key);}/*** 普通缓存放入* param key 键* param value 值* return true成功 false失败*/public boolean set(String key, Object value) {try {redisTemplate.opsForValue().set(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 普通缓存放入并设置时间* param key 键* param value 值* param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期* return true成功 false 失败*/public boolean set(String key, Object value, long time) {try {if (time 0) {redisTemplate.opsForValue().set(key, value, time,TimeUnit.SECONDS);} else {set(key, value);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 递增* param key 键* param delta 要增加几(大于0)*/public long incr(String key, long delta) {if (delta 0) {throw new RuntimeException(递增因子必须大于0);}return redisTemplate.opsForValue().increment(key, delta);}/*** 递减* param key 键* param delta 要减少几(小于0)*/public long decr(String key, long delta) {if (delta 0) {throw new RuntimeException(递减因子必须大于0);}return redisTemplate.opsForValue().increment(key, -delta);} // Map/*** HashGet* param key 键 不能为null* param item 项 不能为null*/public Object hget(String key, String item) {return redisTemplate.opsForHash().get(key, item);}/*** 获取hashKey对应的所有键值* param key 键* return 对应的多个键值*/public MapObject, Object hmget(String key) {return redisTemplate.opsForHash().entries(key);}/*** HashSet* param key 键* param map 对应多个键值*/public boolean hmset(String key, MapString, Object map) {try {redisTemplate.opsForHash().putAll(key, map);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** HashSet 并设置时间* param key 键* param map 对应多个键值* param time 时间(秒)* return true成功 false失败*/public boolean hmset(String key, MapString, Object map, long time) {try {redisTemplate.opsForHash().putAll(key, map);if (time 0) {expire(key, time);}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建** param key 键* param item 项* param value 值* return true 成功 false失败*/public boolean hset(String key, String item, Object value) {try {redisTemplate.opsForHash().put(key, item, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 向一张hash表中放入数据,如果不存在将创建** param key 键* param item 项* param value 值* param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间* return true 成功 false失败*/public 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) {e.printStackTrace();return false;}}/*** 删除hash表中的值** param key 键 不能为null* param item 项 可以使多个 不能为null*/public void hdel(String key, Object... item) {redisTemplate.opsForHash().delete(key, item);}/*** 判断hash表中是否有该项的值** param key 键 不能为null* param item 项 不能为null* return true 存在 false不存在*/public boolean hHasKey(String key, String item) {return redisTemplate.opsForHash().hasKey(key, item);}/*** hash递增 如果不存在,就会创建一个 并把新增后的值返回** param key 键* param item 项* param by 要增加几(大于0)*/public double hincr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, by);}/*** hash递减** param key 键* param item 项* param by 要减少记(小于0)*/public double hdecr(String key, String item, double by) {return redisTemplate.opsForHash().increment(key, item, -by);} // set/*** 根据key获取Set中的所有值* param key 键*/public SetObject sGet(String key) {try {return redisTemplate.opsForSet().members(key);} catch (Exception e) {e.printStackTrace();return null;}}/*** 根据value从一个set中查询,是否存在** param key 键* param value 值* return true 存在 false不存在*/public boolean sHasKey(String key, Object value) {try {return redisTemplate.opsForSet().isMember(key, value);} catch (Exception e) {e.printStackTrace();return false;}}/*** 将数据放入set缓存** param key 键* param values 值 可以是多个* return 成功个数*/public long sSet(String key, Object... values) {try {return redisTemplate.opsForSet().add(key, values);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 将set数据放入缓存** param key 键* param time 时间(秒)* param values 值 可以是多个* return 成功个数*/public long sSetAndTime(String key, long time, Object... values) {try {Long count redisTemplate.opsForSet().add(key, values);if (time 0)expire(key, time);return count;} catch (Exception e) {e.printStackTrace();return 0;}}/*** 获取set缓存的长度** param key 键*/public long sGetSetSize(String key) {try {return redisTemplate.opsForSet().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 移除值为value的** param key 键* param values 值 可以是多个* return 移除的个数*/public long setRemove(String key, Object... values) {try {Long count redisTemplate.opsForSet().remove(key, values);return count;} catch (Exception e) {e.printStackTrace();return 0;}} // list/*** 获取list缓存的内容** param key 键* param start 开始* param end 结束 0 到 -1代表所有值*/public ListObject lGet(String key, long start, long end) {try {return redisTemplate.opsForList().range(key, start, end);} catch (Exception e) {e.printStackTrace();return null;}}/*** 获取list缓存的长度** param key 键*/public long lGetListSize(String key) {try {return redisTemplate.opsForList().size(key);} catch (Exception e) {e.printStackTrace();return 0;}}/*** 通过索引 获取list中的值** param key 键* param index 索引 index0时 0 表头1 第二个元素依次类推index0时-1表尾-2倒数第二个元素依次类推*/public Object lGetIndex(String key, long index) {try {return redisTemplate.opsForList().index(key, index);} catch (Exception e) {e.printStackTrace();return null;}}/*** 将list放入缓存** param key 键* param value 值*/public boolean lSet(String key, Object value) {try {redisTemplate.opsForList().rightPush(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存* param key 键* param value 值* param time 时间(秒)*/public boolean lSet(String key, Object value, long time) {try {redisTemplate.opsForList().rightPush(key, value);if (time 0)expire(key, time);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** param key 键* param value 值* return*/public boolean lSet(String key, ListObject value) {try {redisTemplate.opsForList().rightPushAll(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** param key 键* param value 值* param time 时间(秒)* return*/public boolean lSet(String key, ListObject value, long time) {try {redisTemplate.opsForList().rightPushAll(key, value);if (time 0)expire(key, time);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据索引修改list中的某条数据** param key 键* param index 索引* param value 值* return*/public boolean lUpdateIndex(String key, long index, Object value) {try {redisTemplate.opsForList().set(key, index, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 移除N个值为value** param key 键* param count 移除多少个* param value 值* return 移除的个数*/public long lRemove(String key, long count, Object value) {try {Long remove redisTemplate.opsForList().remove(key, count,value);return remove;} catch (Exception e) {e.printStackTrace();return 0;}} }Redis持久化 Redis 持久化的主要作用是将 Redis 的数据持久化到磁盘中以防止 Redis 在内存中的数据意外丢失或者系统宕机导致的数据丢失保证数据的可靠性和持久性Redis 提供了两种持久化方式分别是 RDB 和 AOF RDB持久化 RDB 持久化将 Redis 在内存中的数据定期写入磁盘中的快照文件可以通过配置定期保存快照的时间间隔也可以手动执行保存操作RDB 持久化的优点是占用内存小恢复数据速度快适用于数据量较大但是对数据实时性要求不高的场景 AOF持久化 AOF 持久化将 Redis 的所有写操作都记录在一个日志文件中可以通过配置日志文件的同步方式确保数据的可靠性和实时性。AOF 持久化的优点是数据实时性高可靠性强适用于数据对实时性要求较高的场景 结语创作不易如果觉得博主的文章赏心悦目还请——点赞收藏⭐️评论
http://www.dnsts.com.cn/news/252090.html

相关文章:

  • 惠州网站建设 翻译免费国内ip
  • 长沙做手机网站建设WordPress文章怎么折叠
  • 网站建设 英语翻译做外汇看什么网站
  • 购物网站排名哪家好沈阳小程序开发定制
  • 济宁市建设工程招投标网站网站建设列表横向
  • 湖南涟钢建设有限公司网站如何建立公司网页
  • 企业网站的推广建议专业网站设计如何提升网页品质
  • 天津seo网站管理阿里云建站教程视频
  • 建设部网站刘赵云网站备案幕布psd
  • 无网站可以做cpc吗广西建设网官方证件信息查询
  • 官网建站网站建筑工程招投标网
  • 宝塔批量建站工具云服务器 虚拟机
  • 网站官网认证怎么做的公司品牌vi设计是什么
  • 源码网站跟自己做的网站区别如何在电商网站做市场调研
  • 常州兼职网站建设建筑网红化
  • 网站标签图标代码WordPress转织梦样式调用
  • 遵义市建设局网站官网怎样用虚拟主机建网站
  • 首页网站怎么做淘宝客网站要备案吗
  • 丹徒网站建设包括哪些重庆建设工程造价协会
  • 河南瑞达建设工程有限公司网站东莞网站优化什么方法
  • saas自助建站莱芜金点子信息港官网
  • 企业手机网站建简易广州网站建设
  • 做网站网站的人是怎么被抓的wordpress 邮件代发
  • 网站建设需要的客户资料张家界网站制作公司
  • 企业建设网站的资金策划长沙p2p网站建设
  • 网站建设合同要存档几年网站开发所需的技术
  • 深圳做网站比较h5游戏源码
  • 网站建设套模板视频58同城网站建设 推广
  • 描述一下网站建设的基本流程做线路板的去哪个网站找工作
  • 镇江网站建设正规网站建设找哪家好