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

阜阳市住房和城乡建设局网站/百度的网页地址

阜阳市住房和城乡建设局网站,百度的网页地址,网站的seo怎么做,有了网站的域名下一步怎么做序 本文主要讲述如何在java里头使用redis进行cas操作。其实呢,redis不像memcached那样显示地支持cas操作,不过它有事务的概念。 准备 redis的docker搭建SpringBoot应用之分布式缓存redis的乐观锁支持 Redis通过使用WATCH, MULTI, and EXEC组成的事务来实…

本文主要讲述如何在java里头使用redis进行cas操作。其实呢,redis不像memcached那样显示地支持cas操作,不过它有事务的概念。

准备

  • redis的docker搭建

  • SpringBoot应用之分布式缓存

redis的乐观锁支持

Redis通过使用WATCH, MULTI, and EXEC组成的事务来实现乐观锁(注意没有用DISCARD),Redis事务没有回滚操作。在SpringDataRedis当中通过RedisTemplate的SessionCallback中来支持(否则事务不生效)。discard的话不需要自己代码处理,callback返回null,成的话,返回非null,依据这个来判断事务是否成功(没有抛异常)。

实例

@Testpublic void cas() throws InterruptedException, ExecutionException {String key = "test-cas-1";ValueOperations<String, String> strOps = redisTemplate.opsForValue();strOps.set(key, "hello");ExecutorService pool  = Executors.newCachedThreadPool();List<Callable<Object>> tasks = new ArrayList<>();for(int i=0;i<5;i++){final int idx = i;tasks.add(new Callable() {@Overridepublic Object call() throws Exception {return redisTemplate.execute(new SessionCallback() {@Overridepublic Object execute(RedisOperations operations) throws DataAccessException {operations.watch(key);String origin = (String) operations.opsForValue().get(key);operations.multi();operations.opsForValue().set(key, origin + idx);Object rs = operations.exec();System.out.println("set:"+origin+idx+" rs:"+rs);return rs;}});}});}List<Future<Object>> futures = pool.invokeAll(tasks);for(Future<Object> f:futures){System.out.println(f.get());}pool.shutdown();pool.awaitTermination(1000, TimeUnit.MILLISECONDS);}

输出

set:hello2 rs:null
set:hello3 rs:[]
set:hello1 rs:null
set:hello4 rs:null
set:hello0 rs:null

查看该值

127.0.0.1:6379> get test-cas-1
"\"hello3\""

SessionCallback

没有在SessionCallback里头执行watch、multi、exec,而是自己单独写

与数据库事务的混淆

template.setEnableTransactionSupport(true);

这个应该是支持数据库的事务成功才执行的意思。

/*** Gets a Redis connection. Is aware of and will return any existing corresponding connections bound to the current* thread, for example when using a transaction manager. Will create a new Connection otherwise, if* {@code allowCreate} is <tt>true</tt>.* * @param factory connection factory for creating the connection* @param allowCreate whether a new (unbound) connection should be created when no connection can be found for the*          current thread* @param bind binds the connection to the thread, in case one was created* @param enableTransactionSupport* @return an active Redis connection*/public static RedisConnection doGetConnection(RedisConnectionFactory factory, boolean allowCreate, boolean bind,boolean enableTransactionSupport) {Assert.notNull(factory, "No RedisConnectionFactory specified");RedisConnectionHolder connHolder = (RedisConnectionHolder) TransactionSynchronizationManager.getResource(factory);if (connHolder != null) {if (enableTransactionSupport) {potentiallyRegisterTransactionSynchronisation(connHolder, factory);}return connHolder.getConnection();}if (!allowCreate) {throw new IllegalArgumentException("No connection found and allowCreate = false");}if (log.isDebugEnabled()) {log.debug("Opening RedisConnection");}RedisConnection conn = factory.getConnection();if (bind) {RedisConnection connectionToBind = conn;if (enableTransactionSupport && isActualNonReadonlyTransactionActive()) {connectionToBind = createConnectionProxy(conn, factory);}connHolder = new RedisConnectionHolder(connectionToBind);TransactionSynchronizationManager.bindResource(factory, connHolder);if (enableTransactionSupport) {potentiallyRegisterTransactionSynchronisation(connHolder, factory);}return connHolder.getConnection();}return conn;}

不要跟本文的乐观锁说的事务混淆在一起。

参考

  • Redis-Transactions-via-Spring-Data-Redis

  • Spring-data-redis 第二天(事务)

http://www.lbrq.cn/news/1403101.html

相关文章:

  • 架子鼓谱那个网站做的好/it培训机构出来能找到工作吗
  • 长沙网站seo/电商培训
  • 网站怎么建设的/百度拍照搜索
  • 网站制作售后/江门百度seo公司
  • 盘锦做网站建设的/如何做好宣传推广
  • 有什么做动画的网站/东莞建设网
  • 做网站赚钱一般做什么/交换链接是什么
  • 网站界面是什么做的/温州seo
  • 旅游网站怎么建设/网站seo招聘
  • 做网站的图片大小是多少/开发定制软件公司
  • 摄影网站模板源码/公司推广方案
  • 北京住房和城乡建设部网站官网/软文大全
  • 珠宝商城网站设计/网站seo优化发布高质量外链
  • 做浏览单的网站有哪些/免费com域名注册永久
  • 政府网站建设怎么谈需求/营销型网站有哪些平台
  • 网站建设有哪些企业/推手平台哪个靠谱
  • 中国装修第一网/企业网站优化服务公司
  • 江苏省建筑工程网/兰州seo快速优化报价
  • 响应式门户网站/关联词有哪些五年级
  • 做商城网站需要办理什么/国外seo比较好的博客网站
  • 小兵cms个人网站模板/seo发包软件
  • 37岁转行做外贸真的很难吗/seo优化专员工作内容
  • 网站/陕西网站建设制作
  • 个人网页制作免费下载/河南网站建设优化技术
  • 正能量网站入口免费安全/营销网站优化推广
  • 做快递网站制作/如何推广网址链接
  • 衍艺武汉网站设计公司/百度推广员工工资怎么样
  • 做宣传册参考的网站/陕西省人民政府
  • 东海建设局网站/丈哥seo博客
  • 十堰哪里有做网站的/广州:推动优化防控措施落地
  • 27.语言模型
  • noetic版本/ubuntu20 通过moveit控制真实机械臂
  • LeetCode100-560和为K的子数组
  • 点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
  • Day8--滑动窗口与双指针--1004. 最大连续1的个数 III,1658. 将 x 减到 0 的最小操作数,3641. 最长半重复子数组
  • 宁波市第八届网络安全大赛初赛(REVERSE-Writeup)