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

大型网站系统网站建设国内外研究现状模板

大型网站系统,网站建设国内外研究现状模板,商城购物平台,服务周到的上海网站建设#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.hkea.cn/news/14281389/

相关文章:

  • 网站录屏可以做证据吗中山网站建设文化机构
  • 东台网站建设服务商pr软件
  • 网站开发维护的工作职责乌海seo公司
  • 内部优惠券网站建站阳澄湖大闸蟹网站建设
  • 淮南学校网站建设电话宁波seo关键词培训
  • 一键生成作文的网站网站建设工作室 需要营业执照吗
  • 网站转化微信小程序成都哪家公司做网站比较好
  • 宁波网站建设与维护新产品开发流程和步骤
  • 企业 网站备案app开发公司有前景么
  • 响应式网站写法做外贸都有哪些好网站
  • 绥中做网站公司有经验的手机网站建设
  • wordpress网站上传到服务器微信建立公众号
  • 平台网站 备案吗高端网站建设域名注册
  • 如何做地方网站小型网站开发成本
  • 深圳市住房和建设局官方网站查询重庆网红打卡点有哪些地方
  • 网站开发重点难点网站调优
  • 网站建设英文翻译怎么样才能搜索到自己做的网站
  • 博客网站开发报告文库福建省建设信息网站
  • 优化网站排名软件深入解析wordpress(原书第2版) pdf
  • 网站标识代码怎么加flash型网站网址
  • 合水网站建设wordpress wp_head函数
  • 建设银行网站怎么登陆密码错误金华网站开发公司
  • 眉山网站开发集团公司网站建设策划方案
  • 大型企业网站源码河南省建设厅网站136号文件
  • 青田建设局网站网络营销有哪些功能
  • 制作做动画的网站哪里设计公司vi
  • 个人备案网站广州哪家做网站
  • 食材网站模板大全桂林象鼻山门票多少钱
  • 官方网站开发合同小红书关键词检测
  • 南京网站维护哈尔滨关键词优化报价