{"id":44488,"date":"2024-12-01T17:34:40","date_gmt":"2024-12-01T09:34:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44488\/"},"modified":"2024-12-01T17:34:40","modified_gmt":"2024-12-01T09:34:40","slug":"redis%e4%bf%9d%e5%ad%98atomicinteger%e5%af%b9%e8%b1%a1%e8%b8%a9%e5%9d%91%e5%8f%8a%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/44488\/","title":{"rendered":"redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;font-family:\">\u300aredis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;font-family:\">\u5bf9\u8c61\u3001redisAtomicInteger<\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<h2>redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751<\/h2>\n<p>redisTemplate \u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u5f02\u5e38\uff1a<\/p>\n<blockquote>\n<p>java.lang.ClassCastException: java.util.concurrent.atomic.AtomicInteger cannot be cast to java.lang.String<br \/>&nbsp;&nbsp; &nbsp;at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:36)<br \/>&nbsp;&nbsp; &nbsp;at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:127)<br \/>&nbsp;&nbsp; &nbsp;at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:235)<br \/>&nbsp;&nbsp; &nbsp;at com.quan.starter.service.impl.RedisServiceImpl.set(RedisServiceImpl.java:139)<\/p>\n<\/blockquote>\n<p>\u8ddf\u8e2a\u6e90\u7801\u53d1\u73b0\u5176\u6267\u884c\u7684\u662f StringRedisSerializer \u7684\u5b9e\u73b0\uff0cserialize\u9ed8\u8ba4\u63a5\u6536\u7684\u53c2\u6570\u7c7b\u578b\u4e3aString \u4ece\u800c\u629b\u51fa\u4ee5\u4e0a\u5f02\u5e38<\/p>\n<p>\u7ecf\u8fc7\u68c0\u67e5\uff0c\u53d1\u73b0\u662fRedisTemplate\u6cdb\u578b\u60f9\u7684\u7978\uff1a<\/p>\n<pre>@Autowired\nprivate RedisTemplate\n   \n     redisTemplate;\n   <\/pre>\n<h3>\u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u53bb\u9664\u6cdb\u578b\uff1a<\/p>\n<pre>@Autowired\nprivate RedisTemplate redisTemplate;<\/pre>\n<p>\u8fd0\u884c\u670d\u52a1\u518d\u6b21\u8ddf\u8e2a\u6e90\u7801\uff0c\u6267\u884c\u7684\u662f DefaultValueOperations \u7684\u5b9e\u73b0\uff0c\u95ee\u9898\u89e3\u51b3<\/p>\n<h2>RedisAtomicInteger\u7684\u4f7f\u7528<\/h2>\n<p>RedisAtomicInteger \u4ece\u540d\u5b57\u4e0a\u6765\u8bf4\u5c31\u662f redis \u7684\u539f\u5b50Integer \u6570\u636e\u7c7b\u578b\uff0c\u7531\u4e8e\u5176\u539f\u5b50\u6027\uff0c\u53ef\u7528\u4e8e\u79d2\u6740\u6d3b\u52a8\u7269\u54c1\u6570\u91cf\u7684\u63a7\u5236\u3002<\/p>\n<p>\u4ee5\u53ca\u4fdd\u8bc1\u987a\u5e8f\u751f\u6210\u6570\u5b57\u3002<\/p>\n<pre>  @Resource\n    RedisTemplate\n   \n     redisTemplate;\n\n\n    \/**\n     * RedisAtomicInteger\n     *\n     * @throws Exception \u5f02\u5e38\n     *\/\n    @Test\n    public void testTransaction1() throws Exception {\n        RedisAtomicInteger redisCount = new RedisAtomicInteger(\"key1\", this.redisTemplate.getConnectionFactory());\n        redisCount.set(0);\n        \/\/ \u521b\u5efa 100 \u4e2a\u7ebf\u7a0b \u5e76\u53d1\u6267\u884c  increment \u64cd\u4f5c\n        ExecutorService pool = Executors.newFixedThreadPool(10);\n        for (int i = 0; i  {\n                \/\/ \u914d\u989d\u7801\u539f\u5b50\u53d8\u91cf\u503c\u589e\u52a0,\u6bcf\u6b21\u589e\u52a01\n                for (int j = 0; j \n\n    <p>\u7ed3\u679c<\/p>\n\n    \n \n \n \n \n <blockquote>\n     \n  \n  \n  \n  \n  <p>.<br>.<br>.<br>pool-2-thread-90: 9989<br>pool-2-thread-61: 9987<br>pool-2-thread-3: 9986<br>pool-2-thread-12: 9990<br>pool-2-thread-25: 9991<br>pool-2-thread-90: 9992<br>pool-2-thread-12: 9994<br>pool-2-thread-61: 9993<br>pool-2-thread-25: 9995<br>pool-2-thread-61: 10000<br>pool-2-thread-12: 9996<br>pool-2-thread-61: 9997<br>pool-2-thread-25: 9998<br>pool-2-thread-12: 9999<\/p>\n    \n \n \n \n \n <\/blockquote>\n\n    <p>\u4ee5\u4e0a\u4e3a\u4e2a\u4eba\u7ecf\u9a8c\uff0c\u5e0c\u671b\u80fd\u7ed9\u5927\u5bb6\u4e00\u4e2a\u53c2\u8003\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u591a\u591a\u652f\u6301golang\u5b66\u4e60\u7f51\u3002<\/p>\n    <p>\u4ee5\u4e0a\u5c31\u662f\u300aredis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8eredis\u7684\u8d44\u6599\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01<\/p>\n   <\/pre>\n<p>    \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Redis\u5bf9\u8c61\u4e0eredisObject\u8d85\u8be6\u7ec6\u5206\u6790\u6e90\u7801\u5c42\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u4f7f\u7528RedisAtomicInteger\u8ba1\u6570\u51fa\u73b0\u5c11\u8ba1\u95ee\u9898\u53ca\u89e3\u51b3\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aredis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751\u53ca\u89e3\u51b3\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u5bf9\u8c61\u3001redisAtomicInteger\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ redis\u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u8e29\u5751 redisTemplate \u4fdd\u5b58AtomicInteger\u5bf9\u8c61\u5f02\u5e38\uff1a java.lang.ClassCastException: java.util.concurrent.atomic.AtomicInteger cannot be cast to java.lang.String&nbsp;&nbsp; &nbsp;at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:36)&nbsp;&nbsp; &nbsp;at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:127)&nbsp;&nbsp; &nbsp;at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:235)&nbsp;&nbsp; &nbsp;at com.quan.starter.service.impl.RedisServiceImpl.set(RedisServiceImpl.java:139) \u8ddf\u8e2a\u6e90\u7801\u53d1\u73b0\u5176\u6267\u884c\u7684\u662f StringRedisSerializer \u7684\u5b9e\u73b0\uff0cserialize\u9ed8\u8ba4\u63a5\u6536\u7684\u53c2\u6570\u7c7b\u578b\u4e3aString \u4ece\u800c\u629b\u51fa\u4ee5\u4e0a\u5f02\u5e38 \u7ecf\u8fc7\u68c0\u67e5\uff0c\u53d1\u73b0\u662fRedisTemplate\u6cdb\u578b\u60f9\u7684\u7978\uff1a @Autowired private RedisTemplate redisTemplate; \u89e3\u51b3\u65b9\u6848 \u53bb\u9664\u6cdb\u578b\uff1a @Autowired private RedisTemplate redisTemplate; \u8fd0\u884c\u670d\u52a1\u518d\u6b21\u8ddf\u8e2a\u6e90\u7801\uff0c\u6267\u884c\u7684\u662f DefaultValueOperations \u7684\u5b9e\u73b0\uff0c\u95ee\u9898\u89e3\u51b3 RedisAtomicInteger\u7684\u4f7f\u7528 RedisAtomicInteger \u4ece\u540d\u5b57\u4e0a\u6765\u8bf4\u5c31\u662f redis \u7684\u539f\u5b50Integer \u6570\u636e\u7c7b\u578b\uff0c\u7531\u4e8e\u5176\u539f\u5b50\u6027\uff0c\u53ef\u7528\u4e8e\u79d2\u6740\u6d3b\u52a8\u7269\u54c1\u6570\u91cf\u7684\u63a7\u5236\u3002 \u4ee5\u53ca\u4fdd\u8bc1\u987a\u5e8f\u751f\u6210\u6570\u5b57\u3002 @Resource RedisTemplate redisTemplate; \/** * RedisAtomicInteger * * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-44488","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=44488"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44488\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}