﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-paulwong-随笔分类-REDIS</title><link>http://www.blogjava.net/paulwong/category/53871.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 19 Oct 2015 04:57:47 GMT</lastBuildDate><pubDate>Mon, 19 Oct 2015 04:57:47 GMT</pubDate><ttl>60</ttl><item><title>REDIS监控</title><link>http://www.blogjava.net/paulwong/archive/2015/10/19/427811.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Mon, 19 Oct 2015 04:39:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2015/10/19/427811.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/427811.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2015/10/19/427811.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/427811.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/427811.html</trackback:ping><description><![CDATA[<a href="http://git.oschina.net/hellovivi/RedisFlag" target="_blank">http://git.oschina.net/hellovivi/RedisFlag</a><img src ="http://www.blogjava.net/paulwong/aggbug/427811.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2015-10-19 12:39 <a href="http://www.blogjava.net/paulwong/archive/2015/10/19/427811.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring Boot使用redis做数据缓存</title><link>http://www.blogjava.net/paulwong/archive/2015/02/25/423020.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Wed, 25 Feb 2015 02:02:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2015/02/25/423020.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/423020.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2015/02/25/423020.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/423020.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/423020.html</trackback:ping><description><![CDATA[<div id="blog_content" style="line-height: 1.8em; font-family: Helvetica, Tahoma, Arial, sans-serif;"><div><h1>1 添加redis支持</h1><p style="margin: 0px; padding: 0px;">在pom.xml中添加</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Xml代码&nbsp;&nbsp;<a title="收藏这段代码" style="color: #108ac6; text-decoration: underline;"><img src="http://wiselyman.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #006699; font-weight: bold;">&lt;</span><span style="color: #006699; font-weight: bold;">dependency</span><span style="color: #006699; font-weight: bold;">&gt;</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #006699; font-weight: bold;">&lt;</span><span style="color: #006699; font-weight: bold;">groupId</span><span style="color: #006699; font-weight: bold;">&gt;</span>org.springframework.boot<span style="color: #006699; font-weight: bold;">&lt;/</span><span style="color: #006699; font-weight: bold;">groupId</span><span style="color: #006699; font-weight: bold;">&gt;</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #006699; font-weight: bold;">&lt;</span><span style="color: #006699; font-weight: bold;">artifactId</span><span style="color: #006699; font-weight: bold;">&gt;</span>spring-boot-starter-redis<span style="color: #006699; font-weight: bold;">&lt;/</span><span style="color: #006699; font-weight: bold;">artifactId</span><span style="color: #006699; font-weight: bold;">&gt;</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #006699; font-weight: bold;">&lt;/</span><span style="color: #006699; font-weight: bold;">dependency</span><span style="color: #006699; font-weight: bold;">&gt;</span>&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>2 redis配置</h1><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Java代码&nbsp;&nbsp;<a title="收藏这段代码" style="color: #108ac6; text-decoration: underline;"><img src="http://wiselyman.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #646464;">@Configuration</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #646464;">@EnableCaching</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #7f0055;">public</span>&nbsp;<span style="color: #7f0055;">class</span>&nbsp;RedisCacheConfig&nbsp;{&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #646464;">@Bean</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">public</span>&nbsp;CacheManager&nbsp;cacheManager(&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #646464;">@SuppressWarnings</span>("rawtypes")&nbsp;RedisTemplate&nbsp;redisTemplate)&nbsp;{&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">return</span>&nbsp;<span style="color: #7f0055;">new</span>&nbsp;RedisCacheManager(redisTemplate);&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #646464;">@Bean</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">public</span>&nbsp;RedisTemplate&lt;String,&nbsp;String&gt;&nbsp;redisTemplate(&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RedisConnectionFactory&nbsp;factory)&nbsp;{&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">final</span>&nbsp;StringRedisTemplate&nbsp;template&nbsp;=&nbsp;<span style="color: #7f0055;">new</span>&nbsp;StringRedisTemplate(factory);&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template.setValueSerializer(<span style="color: #7f0055;">new</span>&nbsp;Jackson2JsonRedisSerializer&lt;SysUser&gt;(&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SysUser.<span style="color: #7f0055;">class</span>));&nbsp;<span style="width: auto; border-width: 0px;">//请注意这里</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">return</span>&nbsp;template;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">}&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>3 redis服务器配置</h1><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Properties代码&nbsp;&nbsp;<a title="收藏这段代码" style="color: #108ac6; text-decoration: underline;"><img src="http://wiselyman.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">#&nbsp;REDIS&nbsp;(RedisProperties)&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.database=&nbsp;#&nbsp;database&nbsp;name&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.host=localhost&nbsp;#&nbsp;server&nbsp;host&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.password=&nbsp;#&nbsp;server&nbsp;password&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.port=<span style="color: #c00000;">6379</span>&nbsp;#&nbsp;connection&nbsp;port&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.pool.max-idle=<span style="color: #c00000;">8</span>&nbsp;#&nbsp;pool&nbsp;settings&nbsp;...&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.pool.min-idle=<span style="color: #c00000;">0</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.pool.max-active=<span style="color: #c00000;">8</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.pool.max-wait=-<span style="color: #c00000;">1</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.sentinel.master=&nbsp;#&nbsp;name&nbsp;of&nbsp;Redis&nbsp;server&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">spring.redis.sentinel.nodes=&nbsp;#&nbsp;comma-separated&nbsp;list&nbsp;of&nbsp;host:port&nbsp;pairs&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>4 应用</h1><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Java代码&nbsp;&nbsp;<a title="收藏这段代码" style="color: #108ac6; text-decoration: underline;"><img src="http://wiselyman.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="width: auto; border-width: 0px;">/**</span>&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="width: auto; border-width: 0px;">*此处的dao操作使用的是spring&nbsp;data&nbsp;jpa,使用@Cacheable可以在任意方法上，*比如@Service或者@Controller的方法上</span>&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="width: auto; border-width: 0px;">*/</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #7f0055;">public</span>&nbsp;<span style="color: #7f0055;">interface</span>&nbsp;SysUserRepo1&nbsp;<span style="color: #7f0055;">extends</span>&nbsp;CustomRepository&lt;SysUser,&nbsp;Long&gt;&nbsp;{&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #646464;">@Cacheable</span>(value&nbsp;=&nbsp;"usercache")&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">public</span>&nbsp;SysUser&nbsp;findByUsername(String&nbsp;username);&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">}&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px;">&nbsp;</p><h1>5 检验</h1><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Java代码&nbsp;&nbsp;<a title="收藏这段代码" style="color: #108ac6; text-decoration: underline;"><img src="http://wiselyman.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #646464;">@Controller</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;"><span style="color: #7f0055;">public</span>&nbsp;<span style="color: #7f0055;">class</span>&nbsp;TestController&nbsp;{&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #646464;">@Autowired</span>&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;SysUserRepo1&nbsp;sysUserRepo1;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #646464;">@RequestMapping</span>("/test")&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">public</span>&nbsp;<span style="color: #646464;">@ResponseBody</span>&nbsp;String&nbsp;test(){&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">final</span>&nbsp;SysUser&nbsp;loaded&nbsp;=&nbsp;sysUserRepo1.findByUsername("wyf");&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">final</span>&nbsp;SysUser&nbsp;cached&nbsp;=&nbsp;sysUserRepo1.findByUsername("wyf");&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055;">return</span>&nbsp;"ok";&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; line-height: 18px; background-color: #fafafa;">}&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">效果如图：</p><p style="margin: 0px; padding: 0px;"><br /><img alt="" src="http://dl2.iteye.com/upload/attachment/0105/9845/65280a9e-6a00-3f6b-a71a-e97da1223e00.png" title="点击查看原始大小图片" width="700" height="439" style="border: 0px; cursor: url(http://www.iteye.com/images/magplus.gif), pointer;" /><br />&nbsp;</p></div></div><img src ="http://www.blogjava.net/paulwong/aggbug/423020.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2015-02-25 10:02 <a href="http://www.blogjava.net/paulwong/archive/2015/02/25/423020.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>几大NOSQL数据库性能比较</title><link>http://www.blogjava.net/paulwong/archive/2015/01/30/422565.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Thu, 29 Jan 2015 16:16:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2015/01/30/422565.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/422565.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2015/01/30/422565.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/422565.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/422565.html</trackback:ping><description><![CDATA[Apache Cassandra NoSQL Performance Benchmarks<br /><a href="http://planetcassandra.org/nosql-performance-benchmarks" target="_blank">http://planetcassandra.org/nosql-performance-benchmarks</a><br /><br />NoSQL Benchmarking<br /><a href="http://www.cubrid.org/blog/dev-platform/nosql-benchmarking/" target="_blank">http://www.cubrid.org/blog/dev-platform/nosql-benchmarking/</a><br /><br /><br /><a href="http://www.badrit.com/blog/2013/11/18/redis-vs-mongodb-performance#.VMpfW2SUfsg" target="_blank">http://www.badrit.com/blog/2013/11/18/redis-vs-mongodb-performance#.VMpfW2SUfsg</a><br /><br />How many requests per second can I get out of Redis?<br /><a href="http://skipperkongen.dk/2013/08/27/how-many-requests-per-second-can-i-get-out-of-redis/" target="_blank">http://skipperkongen.dk/2013/08/27/how-many-requests-per-second-can-i-get-out-of-redis/</a><br /><br />redis性能测试，测试并发性能<br /><a href="http://my.oschina.net/pblack/blog/102394" target="_blank">http://my.oschina.net/pblack/blog/102394</a><img src ="http://www.blogjava.net/paulwong/aggbug/422565.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2015-01-30 00:16 <a href="http://www.blogjava.net/paulwong/archive/2015/01/30/422565.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Architecture for Redis cache &amp; Mongo for persistence</title><link>http://www.blogjava.net/paulwong/archive/2015/01/04/422026.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Sun, 04 Jan 2015 07:50:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2015/01/04/422026.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/422026.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2015/01/04/422026.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/422026.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/422026.html</trackback:ping><description><![CDATA[Caching with Spring Data Redis<br /><a href="http://www.javacodegeeks.com/2013/02/caching-with-spring-data-redis.html" target="_blank">http://www.javacodegeeks.com/2013/02/caching-with-spring-data-redis.html</a><br /><br />Architecture for Redis cache &amp; Mongo for persistence<br /><a href="http://stackoverflow.com/questions/11218941/architecture-for-redis-cache-mongo-for-persistence" target="_blank">http://stackoverflow.com/questions/11218941/architecture-for-redis-cache-mongo-for-persistence</a><br /><br />MongoDB with redis<br /><a href="http://stackoverflow.com/questions/10696463/mongodb-with-redis/10721249#10721249" target="_blank">http://stackoverflow.com/questions/10696463/mongodb-with-redis/10721249#10721249</a><br /><br />Caching Data in Spring Using Redis<br /><a href="http://caseyscarborough.com/blog/2014/12/18/caching-data-in-spring-using-redis/" target="_blank">http://caseyscarborough.com/blog/2014/12/18/caching-data-in-spring-using-redis/</a><br /><br />Springside Redis<br /><a href="https://github.com/springside/springside4/wiki/Redis" target="_blank">https://github.com/springside/springside4/wiki/Redis</a><br /><br />Spring Cache注解+Redis<br /><a href="http://hanqunfeng.iteye.com/blog/2176172" target="_blank">http://hanqunfeng.iteye.com/blog/2176172</a><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.blogjava.net/paulwong/aggbug/422026.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2015-01-04 15:50 <a href="http://www.blogjava.net/paulwong/archive/2015/01/04/422026.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SPRING-SESSION</title><link>http://www.blogjava.net/paulwong/archive/2014/11/19/420309.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Wed, 19 Nov 2014 10:23:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/11/19/420309.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/420309.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/11/19/420309.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/420309.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/420309.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: HTTP SESSION的管理通常是由容器来做，但如果是在PAAS环境下，服务器不能做变更，则需要由WEB应用来做处理HTTP SESSION。同样，如果是分布式的环境下，SESSION的管理也会带来性能问题。SPRING推出了处理SESSION的框架：SPRING-SESSION。SPRING会重写HTTP SESSION的那一套，使用SESSION也同样还是用Code ...&nbsp;&nbsp;<a href='http://www.blogjava.net/paulwong/archive/2014/11/19/420309.html'>阅读全文</a><img src ="http://www.blogjava.net/paulwong/aggbug/420309.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2014-11-19 18:23 <a href="http://www.blogjava.net/paulwong/archive/2014/11/19/420309.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Redis 集群解决方案 Codis</title><link>http://www.blogjava.net/paulwong/archive/2014/11/09/419813.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Sun, 09 Nov 2014 01:28:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/11/09/419813.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/419813.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/11/09/419813.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/419813.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/419813.html</trackback:ping><description><![CDATA[<p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;"><a href="https://github.com/wandoulabs/codis" target="_blank">https://github.com/wandoulabs/codis</a><br /><br />Codis 是一个分布式&nbsp;<a target="_blank" href="http://www.oschina.net/p/redis" style="margin: 0px; padding: 0px; color: #3e62a6; outline: 0px;">Redis</a>&nbsp;解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 (不支持的命令列表), 上层应用可以像使用单机的 Redis 一样使用, Codis 底层会处理请求的转发, 不停机的数据迁移等工作, 所有后边的一切事情, 对于前面的客户端来说是透明的, 可以简单的认为后边连接的是一个内存无限大的 Redis 服务.</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">Codis 由四部分组成:</p><ul list-paddingleft-2"="" style="margin: 0.5em 0px 0.5em 1.5em; padding: 0px; list-style-position: inside; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">Codis Proxy &nbsp; (codis-proxy)</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">Codis Manager (codis-config)</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">Codis Redis &nbsp; (codis-server)</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">ZooKeeper</p></li></ul><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">codis-proxy 是客户端连接的 Redis 代理服务, codis-proxy 本身实现了 Redis 协议, 表现得和一个原生的 Redis 没什么区别 (就像&nbsp;<a target="_blank" href="http://www.oschina.net/p/twemproxy" style="margin: 0px; padding: 0px; color: #3e62a6; outline: 0px;">Twemproxy</a>), 对于一个业务来说, 可以部署多个 codis-proxy, codis-proxy 本身是无状态的.</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">codis-config 是 Codis 的管理工具, 支持包括, 添加/删除 Redis 节点, 添加/删除 Proxy 节点, 发起数据迁移等操作. codis-config 本身还自带了一个 http server, 会启动一个 dashboard, 用户可以直接在浏览器上观察 Codis 集群的运行状态.</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">codis-server 是 Codis 项目维护的一个 Redis 分支, 基于 2.8.13 开发, 加入了 slot 的支持和原子的数据迁移指令. Codis 上层的 codis-proxy 和 codis-config 只能和这个版本的 Redis 交互才能正常运行.</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">Codis 依赖 ZooKeeper 来存放数据路由表和 codis-proxy 节点的元信息, codis-config 发起的命令都会通过 ZooKeeper 同步到各个存活的 codis-proxy.</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">Codis 支持按照 Namespace 区分不同的产品, 拥有不同的 product name 的产品, 各项配置都不会冲突.</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">目前 Codis 已经是稳定阶段，目前<a target="_blank" href="http://www.wandoujia.com/" style="margin: 0px; padding: 0px; color: #3e62a6; outline: 0px;">豌豆荚</a>已经在使用该系统。</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">架构：</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;"><img src="http://static.oschina.net/uploads/img/201411/09075151_ofLG.png" alt="Snapshot1" style="margin: 0px; padding: 0px; border: 0px; max-width: 100%;" /></p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">特性：</p><ul list-paddingleft-2"="" style="margin: 0.5em 0px 0.5em 1.5em; padding: 0px; list-style-position: inside; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">自动平衡</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">使用非常简单</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">图形化的面板和管理工具</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">支持绝大多数 Redis 命令，完全兼容&nbsp;<a target="_blank" href="http://www.oschina.net/p/twemproxy" style="margin: 0px; padding: 0px; color: #3e62a6; outline: 0px;">twemproxy</a></p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">支持 Redis 原生客户端</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">安全而且透明的数据移植，可根据需要轻松添加和删除节点</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">提供命令行接口</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">RESTful APIs</p></li></ul><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">安装：</p><ul list-paddingleft-2"="" style="margin: 0.5em 0px 0.5em 1.5em; padding: 0px; list-style-position: inside; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">Install go</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">go get github.com/wandoulabs/codis</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">cd codis</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">./bootstrap.sh</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">make gotest</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">cd sample</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px 0px 10px; padding: 0px; display: inline;">follow instructions in usage.md</p></li></ul><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">界面截图：</p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">Dashboard<img src="http://static.oschina.net/uploads/img/201411/09075152_psF3.png" alt="main" style="margin: 0px; padding: 0px; border: 0px; max-width: 100%;" /></p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">Migrate<img src="http://static.oschina.net/uploads/img/201411/09075154_nMX7.png" alt="migrate" style="margin: 0px; padding: 0px; border: 0px; max-width: 100%;" /></p><p style="margin: 0px 0px 10px; padding: 0px; font-family: Verdana, sans-serif, 宋体; font-size: 13px; line-height: 21.3333320617676px; background-color: #ffffff;">Slots<img src="http://static.oschina.net/uploads/img/201411/09075157_16s4.png" alt="slots" style="margin: 0px; padding: 0px; border: 0px; max-width: 100%;" /></p><img src ="http://www.blogjava.net/paulwong/aggbug/419813.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2014-11-09 09:28 <a href="http://www.blogjava.net/paulwong/archive/2014/11/09/419813.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍</title><link>http://www.blogjava.net/paulwong/archive/2014/09/11/417845.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Thu, 11 Sep 2014 12:33:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/09/11/417845.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/417845.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/09/11/417845.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/417845.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/417845.html</trackback:ping><description><![CDATA[<p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">在这里对jedis关于事务、管道和分布式的调用方式做一个简单的介绍和对比：</p><h3>一、普通同步方式</h3><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">最简单和基础的调用方式，</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test1Normal()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Jedis&nbsp;jedis&nbsp;=&nbsp;new&nbsp;Jedis("localhost"); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;result&nbsp;=&nbsp;jedis.set("n"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Simple&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;jedis.disconnect(); }</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">很简单吧，每次<code style="padding: 1px 3px; margin: 0px 2px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, 'monospace !important'; border: 1px solid #eeeeee; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: break-all; color: #40aa53; background-color: #fcfcfc;">set</code>之后都可以返回结果，标记是否成功。</p><h3>二、事务方式(Transactions)</h3><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">redis的事务很简单，他主要目的是保障，一个client发起的事务中的命令可以连续的执行，而中间不会插入其他client的命令。</p><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">看下面例子：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test2Trans()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Jedis&nbsp;jedis&nbsp;=&nbsp;new&nbsp;Jedis("localhost"); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;Transaction&nbsp;tx&nbsp;=&nbsp;jedis.multi(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.set("t"&nbsp;+&nbsp;i,&nbsp;"t"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;tx.exec(); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Transaction&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;jedis.disconnect(); }</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">我们调用<code style="padding: 1px 3px; margin: 0px 2px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, 'monospace !important'; border: 1px solid #eeeeee; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: break-all; color: #40aa53; background-color: #fcfcfc;">jedis.watch(&#8230;)</code>方法来监控key，如果调用后key值发生变化，则整个事务会执行失败。另外，事务中某个操作失败，并不会回滚其他操作。这一点需要注意。还有，我们可以使用<code style="padding: 1px 3px; margin: 0px 2px; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, 'monospace !important'; border: 1px solid #eeeeee; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: break-all; color: #40aa53; background-color: #fcfcfc;">discard()</code>方法来取消事务。</p><h3>三、管道(Pipelining)</h3><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">有时，我们需要采用异步方式，一次发送多个指令，不同步等待其返回结果。这样可以取得非常好的执行效率。这就是管道，调用方法如下：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test3Pipelined()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Jedis&nbsp;jedis&nbsp;=&nbsp;new&nbsp;Jedis("localhost"); &nbsp;&nbsp;&nbsp;&nbsp;Pipeline&nbsp;pipeline&nbsp;=&nbsp;jedis.pipelined(); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set("p"&nbsp;+&nbsp;i,&nbsp;"p"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;jedis.disconnect(); }</pre><h3>四、管道中调用事务</h3><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">就Jedis提供的方法而言，是可以做到在管道中使用事务，其代码如下：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test4combPipelineTrans()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;jedis&nbsp;=&nbsp;new&nbsp;Jedis("localhost");&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;Pipeline&nbsp;pipeline&nbsp;=&nbsp;jedis.pipelined(); &nbsp;&nbsp;&nbsp;&nbsp;pipeline.multi(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set(""&nbsp;+&nbsp;i,&nbsp;""&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;pipeline.exec(); &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined&nbsp;transaction:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;jedis.disconnect(); }</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">但是经测试（见本文后续部分），发现其效率和单独使用事务差不多，甚至还略微差点。</p><h3>五、分布式直连同步调用</h3><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test5shardNormal()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;List&lt;JedisShardInfo&gt;&nbsp;shards&nbsp;=&nbsp;Arrays.asList( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6379), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6380));  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedis&nbsp;sharding&nbsp;=&nbsp;new&nbsp;ShardedJedis(shards);  &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;result&nbsp;=&nbsp;sharding.set("sn"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Simple@Sharing&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds");  &nbsp;&nbsp;&nbsp;&nbsp;sharding.disconnect(); }</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">这个是分布式直接连接，并且是同步调用，每步执行都返回执行结果。类似地，还有异步管道调用。</p><h3>六、分布式直连异步调用</h3><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test6shardpipelined()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;List&lt;JedisShardInfo&gt;&nbsp;shards&nbsp;=&nbsp;Arrays.asList( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6379), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6380));  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedis&nbsp;sharding&nbsp;=&nbsp;new&nbsp;ShardedJedis(shards);  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedisPipeline&nbsp;pipeline&nbsp;=&nbsp;sharding.pipelined(); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set("sp"&nbsp;+&nbsp;i,&nbsp;"p"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined@Sharing&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds");  &nbsp;&nbsp;&nbsp;&nbsp;sharding.disconnect(); }</pre><h3>七、分布式连接池同步调用</h3><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">如果，你的分布式调用代码是运行在线程中，那么上面两个直连调用方式就不合适了，因为直连方式是非线程安全的，这个时候，你就必须选择连接池调用。</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test7shardSimplePool()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;List&lt;JedisShardInfo&gt;&nbsp;shards&nbsp;=&nbsp;Arrays.asList( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6379), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6380));  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedisPool&nbsp;pool&nbsp;=&nbsp;new&nbsp;ShardedJedisPool(new&nbsp;JedisPoolConfig(),&nbsp;shards);  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedis&nbsp;one&nbsp;=&nbsp;pool.getResource();  &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;result&nbsp;=&nbsp;one.set("spn"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;pool.returnResource(one); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Simple@Pool&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds");  &nbsp;&nbsp;&nbsp;&nbsp;pool.destroy(); }</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">上面是同步方式，当然还有异步方式。</p><h3>八、分布式连接池异步调用</h3><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">@Test public&nbsp;void&nbsp;test8shardPipelinedPool()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;List&lt;JedisShardInfo&gt;&nbsp;shards&nbsp;=&nbsp;Arrays.asList( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6379), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6380));  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedisPool&nbsp;pool&nbsp;=&nbsp;new&nbsp;ShardedJedisPool(new&nbsp;JedisPoolConfig(),&nbsp;shards);  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedis&nbsp;one&nbsp;=&nbsp;pool.getResource();  &nbsp;&nbsp;&nbsp;&nbsp;ShardedJedisPipeline&nbsp;pipeline&nbsp;=&nbsp;one.pipelined();  &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set("sppn"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;pool.returnResource(one); &nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined@Pool&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;pool.destroy(); }</pre><h3>九、需要注意的地方</h3><ol style="padding: 0px; margin: 10px 20px; line-height: 22.5px; color: #333333; font-family: Verdana, sans-serif, 宋体; font-size: 13px; background-color: #ffffff;"><li style="padding: 0px; margin: 0px;"><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-family: '\1a\1a宋体', Arial, sans-serif;">事务和管道都是异步模式。在事务和管道中不能同步查询结果。比如下面两个调用，都是不允许的：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; background: #f6f6f6;">&nbsp;Transaction&nbsp;tx&nbsp;=&nbsp;jedis.multi(); &nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.set("t"&nbsp;+&nbsp;i,&nbsp;"t"&nbsp;+&nbsp;i); &nbsp;} &nbsp;System.out.println(tx.get("t1000").get());&nbsp;&nbsp;//不允许  &nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;tx.exec();  &nbsp;&#8230; &nbsp;&#8230;  &nbsp;Pipeline&nbsp;pipeline&nbsp;=&nbsp;jedis.pipelined(); &nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set("p"&nbsp;+&nbsp;i,&nbsp;"p"&nbsp;+&nbsp;i); &nbsp;} &nbsp;System.out.println(pipeline.get("p1000").get());&nbsp;//不允许  &nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll();</pre></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-family: '\1a\1a宋体', Arial, sans-serif;">事务和管道都是异步的，个人感觉，在管道中再进行事务调用，没有必要，不如直接进行事务模式。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-family: '\1a\1a宋体', Arial, sans-serif;">分布式中，连接池的性能比直连的性能略好(见后续测试部分)。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-family: '\1a\1a宋体', Arial, sans-serif;">分布式调用中不支持事务。</p><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-family: '\1a\1a宋体', Arial, sans-serif;">因为事务是在服务器端实现，而在分布式中，每批次的调用对象都可能访问不同的机器，所以，没法进行事务。</p></li></ol><h3>十、测试</h3><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">运行上面的代码，进行测试，其结果如下：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">Simple&nbsp;SET:&nbsp;5.227&nbsp;seconds  Transaction&nbsp;SET:&nbsp;0.5&nbsp;seconds Pipelined&nbsp;SET:&nbsp;0.353&nbsp;seconds Pipelined&nbsp;transaction:&nbsp;0.509&nbsp;seconds  Simple@Sharing&nbsp;SET:&nbsp;5.289&nbsp;seconds Pipelined@Sharing&nbsp;SET:&nbsp;0.348&nbsp;seconds  Simple@Pool&nbsp;SET:&nbsp;5.039&nbsp;seconds Pipelined@Pool&nbsp;SET:&nbsp;0.401&nbsp;seconds</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">另外，经测试分布式中用到的机器越多，调用会越慢。上面是2片，下面是5片：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">Simple@Sharing&nbsp;SET:&nbsp;5.494&nbsp;seconds Pipelined@Sharing&nbsp;SET:&nbsp;0.51&nbsp;seconds Simple@Pool&nbsp;SET:&nbsp;5.223&nbsp;seconds Pipelined@Pool&nbsp;SET:&nbsp;0.518&nbsp;seconds</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">下面是10片：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">Simple@Sharing&nbsp;SET:&nbsp;5.9&nbsp;seconds Pipelined@Sharing&nbsp;SET:&nbsp;0.794&nbsp;seconds Simple@Pool&nbsp;SET:&nbsp;5.624&nbsp;seconds Pipelined@Pool&nbsp;SET:&nbsp;0.762&nbsp;seconds</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">下面是100片：</p><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">Simple@Sharing&nbsp;SET:&nbsp;14.055&nbsp;seconds Pipelined@Sharing&nbsp;SET:&nbsp;8.185&nbsp;seconds Simple@Pool&nbsp;SET:&nbsp;13.29&nbsp;seconds Pipelined@Pool&nbsp;SET:&nbsp;7.767&nbsp;seconds</pre><p style="padding: 0px 0px 8px; margin: 8px 0px; line-height: 1.8em; letter-spacing: 0.5px; font-size: 13px; font-family: '\1a\1a宋体', Arial, sans-serif; color: #959595; background-color: #ffffff;">分布式中，连接池方式调用不但线程安全外，根据上面的测试数据，也可以看出连接池比直连的效率更好。</p><h3>十一、完整的测试代码</h3><pre style="padding: 5px; margin-top: 10px; margin-bottom: 10px; margin-left: 20px; line-height: 18px; font-size: 9pt; font-family: 'Courier New', Arial; border-width: 1px 1px 1px 5px; border-style: solid; border-color: #dddddd #dddddd #dddddd #6ce26c; color: #333333; background: #f6f6f6;">package&nbsp;com.example.nosqlclient;  import&nbsp;java.util.Arrays; import&nbsp;java.util.List;  import&nbsp;org.junit.AfterClass; import&nbsp;org.junit.BeforeClass; import&nbsp;org.junit.Test;  import&nbsp;redis.clients.jedis.Jedis; import&nbsp;redis.clients.jedis.JedisPoolConfig; import&nbsp;redis.clients.jedis.JedisShardInfo; import&nbsp;redis.clients.jedis.Pipeline; import&nbsp;redis.clients.jedis.ShardedJedis; import&nbsp;redis.clients.jedis.ShardedJedisPipeline; import&nbsp;redis.clients.jedis.ShardedJedisPool; import&nbsp;redis.clients.jedis.Transaction;  import&nbsp;org.junit.FixMethodOrder; import&nbsp;org.junit.runners.MethodSorters;  @FixMethodOrder(MethodSorters.NAME_ASCENDING) public&nbsp;class&nbsp;TestJedis&nbsp;{  &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;Jedis&nbsp;jedis; &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;ShardedJedis&nbsp;sharding; &nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;ShardedJedisPool&nbsp;pool;  &nbsp;&nbsp;&nbsp;&nbsp;@BeforeClass &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;setUpBeforeClass()&nbsp;throws&nbsp;Exception&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;JedisShardInfo&gt;&nbsp;shards&nbsp;=&nbsp;Arrays.asList( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6379), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new&nbsp;JedisShardInfo("localhost",6379));&nbsp;//使用相同的ip:port,仅作测试   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jedis&nbsp;=&nbsp;new&nbsp;Jedis("localhost");&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sharding&nbsp;=&nbsp;new&nbsp;ShardedJedis(shards);  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool&nbsp;=&nbsp;new&nbsp;ShardedJedisPool(new&nbsp;JedisPoolConfig(),&nbsp;shards); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@AfterClass &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;tearDownAfterClass()&nbsp;throws&nbsp;Exception&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jedis.disconnect(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sharding.disconnect(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool.destroy(); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test1Normal()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;result&nbsp;=&nbsp;jedis.set("n"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Simple&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test2Trans()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Transaction&nbsp;tx&nbsp;=&nbsp;jedis.multi(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tx.set("t"&nbsp;+&nbsp;i,&nbsp;"t"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//System.out.println(tx.get("t1000").get());  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;tx.exec(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Transaction&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test3Pipelined()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pipeline&nbsp;pipeline&nbsp;=&nbsp;jedis.pipelined(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set("p"&nbsp;+&nbsp;i,&nbsp;"p"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//System.out.println(pipeline.get("p1000").get()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test4combPipelineTrans()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pipeline&nbsp;pipeline&nbsp;=&nbsp;jedis.pipelined(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.multi(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set(""&nbsp;+&nbsp;i,&nbsp;""&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.exec(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined&nbsp;transaction:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test5shardNormal()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;result&nbsp;=&nbsp;sharding.set("sn"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Simple@Sharing&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test6shardpipelined()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShardedJedisPipeline&nbsp;pipeline&nbsp;=&nbsp;sharding.pipelined(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set("sp"&nbsp;+&nbsp;i,&nbsp;"p"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined@Sharing&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test7shardSimplePool()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShardedJedis&nbsp;one&nbsp;=&nbsp;pool.getResource();  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;result&nbsp;=&nbsp;one.set("spn"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool.returnResource(one); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Simple@Pool&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;}  &nbsp;&nbsp;&nbsp;&nbsp;@Test &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;test8shardPipelinedPool()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShardedJedis&nbsp;one&nbsp;=&nbsp;pool.getResource();  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ShardedJedisPipeline&nbsp;pipeline&nbsp;=&nbsp;one.pipelined();  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;start&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;100000;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pipeline.set("sppn"&nbsp;+&nbsp;i,&nbsp;"n"&nbsp;+&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Object&gt;&nbsp;results&nbsp;=&nbsp;pipeline.syncAndReturnAll(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;end&nbsp;=&nbsp;System.currentTimeMillis(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pool.returnResource(one); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Pipelined@Pool&nbsp;SET:&nbsp;"&nbsp;+&nbsp;((end&nbsp;-&nbsp;start)/1000.0)&nbsp;+&nbsp;"&nbsp;seconds"); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><div></div><img src ="http://www.blogjava.net/paulwong/aggbug/417845.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2014-09-11 20:33 <a href="http://www.blogjava.net/paulwong/archive/2014/09/11/417845.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>为什么使用 Redis及其产品定位</title><link>http://www.blogjava.net/paulwong/archive/2014/08/26/417323.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Tue, 26 Aug 2014 01:04:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/08/26/417323.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/417323.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/08/26/417323.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/417323.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/417323.html</trackback:ping><description><![CDATA[<h2>传统MySQL+ Memcached架构遇到的问题</h2><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">实际MySQL是适合进行海量数据存储的，通过Memcached将热点数据加载到cache，加速访问，很多公司都曾经使用过这样的架构，但随着业务数据量的不断增加，和访问量的持续增长，我们遇到了很多问题：</p><ol style="padding: 0px; margin: 10px 20px; line-height: 22.5px; color: #333333; font-family: Verdana, sans-serif, 宋体; font-size: 13px; background-color: #ffffff;"><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">MySQL需要不断进行拆库拆表，Memcached也需不断跟着扩容，扩容和维护工作占据大量开发时间。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Memcached与MySQL数据库数据一致性问题。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Memcached数据命中率低或down机，大量访问直接穿透到DB，MySQL无法支撑。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">跨机房cache同步问题。</p></li></ol><h2>众多NoSQL百花齐放，如何选择</h2><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">最近几年，业界不断涌现出很多各种各样的NoSQL产品，那么如何才能正确地使用好这些产品，最大化地发挥其长处，是我们需要深入研究和思考的问 题，实际归根结底最重要的是了解这些产品的定位，并且了解到每款产品的tradeoffs，在实际应用中做到扬长避短，总体上这些NoSQL主要用于解决 以下几种问题</p><ol style="padding: 0px; margin: 10px 20px; line-height: 22.5px; color: #333333; font-family: Verdana, sans-serif, 宋体; font-size: 13px; background-color: #ffffff;"><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">少量数据存储，高速读写访问。此类产品通过数据全部in-momery 的方式来保证高速访问，同时提供数据落地的功能，实际这正是Redis最主要的适用场景。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">海量数据存储，分布式系统支持，数据一致性保证，方便的集群节点添加/删除。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">这方面最具代表性的是dynamo和bigtable 2篇论文所阐述的思路。前者是一个完全无中心的设计，节点之间通过gossip方式传递集群信息，数据保证最终一致性，后者是一个中心化的方案设计，通过 类似一个分布式锁服务来保证强一致性,数据写入先写内存和redo log，然后定期compat归并到磁盘上，将随机写优化为顺序写，提高写入性能。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Schema free，auto-sharding等。比如目前常见的一些文档数据库都是支持schema-free的，直接存储json格式数据，并且支持auto-sharding等功能，比如mongodb。</p></li></ol><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">面对这些不同类型的NoSQL产品,我们需要根据我们的业务场景选择最合适的产品。</p><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br style="padding: 0px; margin: 0px;" /></p><h2>Redis适用场景，如何正确的使用</h2><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">前面已经分析过，Redis最适合所有数据in-momory的场景，虽然Redis也提供持久化功能，但实际更多的是一个disk-backed 的功能，跟传统意义上的持久化有比较大的差别，那么可能大家就会有疑问，似乎Redis更像一个加强版的Memcached，那么何时使用 Memcached,何时使用Redis呢？</p><h2>Redis与Memcached的比较</h2><ol style="padding: 0px; margin: 10px 20px; line-height: 22.5px; color: #333333; font-family: Verdana, sans-serif, 宋体; font-size: 13px; background-color: #ffffff;"><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">网络IO模型</p></li><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Memcached是多线程，非阻塞IO复用的网络模型，分为监听主线程和worker子线程，监听线程监听网络连接，接受请求后，将连接 描述字pipe 传递给worker线程，进行读写IO, 网络层使用libevent封装的事件库，多线程模型可以发挥多核作用，但是引入了cache coherency和锁的问题，比如，Memcached最常用的stats 命令，实际Memcached所有操作都要对这个全局变量加锁，进行计数等工作，带来了性能损耗。</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;"><a href="http://static.oschina.net/uploads/img/201408/23155831_gzSR.jpg" target="_blank" style="padding: 0px; margin: 0px; color: #ff8373; outline: 0px; font-size: 12px;"><img alt="" src="http://static.oschina.net/uploads/img/201408/23155831_gzSR.jpg" style="padding: 5px; margin: 10px 0px; border: 1px solid #dddddd; max-width: 640px; cursor: pointer; background: #f4f7f9;" /></a></p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">（Memcached网络IO模型）</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Redis使用单线程的IO复用模型，自己封装了一个简单的AeEvent事件处理框架，主要实现了epoll、kqueue和 select，对于单纯只有IO操作来说，单线程可以将速度优势发挥到最大，但是Redis也提供了一些简单的计算功能，比如排序、聚合等，对于这些操 作，单线程模型实际会严重影响整体吞吐量，CPU计算过程中，整个IO调度都是被阻塞住的。</p><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">内存管理方面</p></li><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Memcached使用预分配的内存池的方式，使用slab和大小不同的chunk来管理内存，Item根据大小选择合适的chunk存 储，内存池的方式可以省去申请/释放内存的开销，并且能减小内存碎片产生，但这种方式也会带来一定程度上的空间浪费，并且在内存仍然有很大空间时，新的数 据也可能会被剔除，原因可以参考Timyang的文章：<span style="padding: 0px; margin: 0px; text-decoration: underline;"><a href="http://timyang.net/data/Memcached-lru-evictions/" rel="nofollow" style="padding: 0px; margin: 0px; color: #ff8373; outline: 0px; font-size: 12px;">http://timyang.net/data/Memcached-lru-evictions/</a></span></p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Redis使用现场申请内存的方式来存储数据，并且很少使用free-list等方式来优化内存分配，会在一定程度上存在内存碎 片，Redis跟据存储命令参数，会把带过期时间的数据单独存放在一起，并把它们称为临时数据，非临时数据是永远不会被剔除的，即便物理内存不够，导致 swap也不会剔除任何非临时数据（但会尝试剔除部分临时数据），这点上Redis更适合作为存储而不是cache。</p><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">数据一致性问题</p></li><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Memcached提供了cas命令，可以保证多个并发访问操作同一份数据的一致性问题。 Redis没有提供cas 命令，并不能保证这点，不过Redis提供了事务的功能，可以保证一串 命令的原子性，中间不会被任何操作打断。</p><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">存储方式及其它方面</p></li><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Memcached基本只支持简单的key-value存储，不支持枚举，不支持持久化和复制等功能</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Redis除key/value之外，还支持list,set,sorted set,hash等众多数据结构，提供了KEYS</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">进行枚举操作，但不能在线上使用，如果需要枚举线上数据，Redis提供了工具可以直接扫描其dump文件，枚举出所有数据，Redis还同时提供了持久化和复制等功能。</p><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">关于不同语言的客户端支持</p></li><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">在不同语言的客户端方面，Memcached和Redis都有丰富的第三方客户端可供选择，不过因为Memcached发展的时间更久一 些，目前看在客户端支持方面，Memcached的很多客户端更加成熟稳定，而Redis由于其协议本身就比Memcached复杂，加上作者不断增加新 的功能等，对应第三方客户端跟进速度可能会赶不上，有时可能需要自己在第三方客户端基础上做些修改才能更好的使用。</p></ol><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">根据以上比较不难看出，当我们不希望数据被踢出，或者需要除key/value之外的更多数据类型时，或者需要落地功能时，使用Redis比使用Memcached更合适。</p><h2>关于Redis的一些周边功能</h2><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">Redis除了作为存储之外还提供了一些其它方面的功能，比如聚合计算、pubsub、scripting等，对于此类功能需要了解其实现原理，清 楚地了解到它的局限性后，才能正确的使用，比如pubsub功能，这个实际是没有任何持久化支持的，消费方连接闪断或重连之间过来的消息是会全部丢失的， 又比如聚合计算和scripting等功能受Redis单线程模型所限，是不可能达到很高的吞吐量的，需要谨慎使用。</p><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">总的来说Redis作者是一位非常勤奋的开发者，可以经常看到作者在尝试着各种不同的新鲜想法和思路，针对这些方面的功能就要求我们需要深入了解后再使用。</p><h2>总结：</h2><ol style="padding: 0px; margin: 10px 20px; line-height: 22.5px; color: #333333; font-family: Verdana, sans-serif, 宋体; font-size: 13px; background-color: #ffffff;"><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Redis使用最佳方式是全部数据in-memory。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Redis更多场景是作为Memcached的替代者来使用。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">当需要除key/value之外的更多数据类型支持时，使用Redis更合适。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">当存储的数据不能被剔除时，使用Redis更合适。</p></li></ol><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">后续关于Redis文章计划：</p><ol style="padding: 0px; margin: 10px 20px; line-height: 22.5px; color: #333333; font-family: Verdana, sans-serif, 宋体; font-size: 13px; background-color: #ffffff;"><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Redis数据类型与容量规划。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">如何根据业务场景搭建稳定，可靠，可扩展的Redis集群。</p></li><li style="padding: 0px; margin: 0px;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Redis参数，代码优化及二次开发基础实践。</p></li></ol><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;"><strong style="padding: 0px; margin: 0px;">关于作者</strong></p><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: Verdana, sans-serif, 宋体; background-color: #ffffff;">田琪，目前负责新浪微博平台底层架构与研发工作，之前曾担任搜狐白社会实时游戏平台核心架构工作，主要关注webgame, 分布式存储，nosql 和 erlang 等领域，目前主要从事mysql源代码的一些深入研究工作，浪微博：<a href="http://weibo.com/bachmozart" rel="nofollow" style="padding: 0px; margin: 0px; color: #ff8373; outline: 0px; font-size: 12px;">http://weibo.com/bachmozart</a>。</p><img src ="http://www.blogjava.net/paulwong/aggbug/417323.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2014-08-26 09:04 <a href="http://www.blogjava.net/paulwong/archive/2014/08/26/417323.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring对HttpSession的重新封闭</title><link>http://www.blogjava.net/paulwong/archive/2014/08/19/417090.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Tue, 19 Aug 2014 01:13:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/08/19/417090.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/417090.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/08/19/417090.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/417090.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/417090.html</trackback:ping><description><![CDATA[<a href="https://github.com/spring-projects/spring-session/tree/master/samples" target="_blank">https://github.com/spring-projects/spring-session/tree/master/samples</a><img src ="http://www.blogjava.net/paulwong/aggbug/417090.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2014-08-19 09:13 <a href="http://www.blogjava.net/paulwong/archive/2014/08/19/417090.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Redisclient 1.0 发布，Java 的 Redis 客户端 GUI 工具</title><link>http://www.blogjava.net/paulwong/archive/2014/07/04/415472.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Fri, 04 Jul 2014 13:47:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/07/04/415472.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/415472.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/07/04/415472.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/415472.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/415472.html</trackback:ping><description><![CDATA[<p style="margin: 0px 0px 15pt; padding: 0px; font-family: Verdana, sans-serif, 宋体; line-height: 22.399999618530273px; background-color: #ffffff;">redisclient 1.0 正式发布，适用于多个 Redis 版本，主要更新内容如下：<br style="margin: 0px; padding: 0px;" /></p><ol list-paddingleft-2"="" style="margin: 0.5em 0px 0.5em 1.5em; padding: 0px; list-style-position: inside; font-family: Verdana, sans-serif, 宋体; line-height: 22.399999618530273px; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">管理 Redis 服务器</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">管理 Redis data favorite</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">管理 Redis 数据<br style="margin: 0px; padding: 0px;" /></p></li><ul list-paddingleft-2"="" style="margin: 0.5em 0px 0.5em 1.5em; padding: 0px; list-style-type: disc; list-style-position: inside;"><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">新 Redis 数据：string, list, hash, set, sorted set</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">重命名 Redis 数据</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">删除 Redis 数据</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">更新 Redis 数据</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">剪切，复制，粘贴 Redis 数据</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">导入，导出 Redis 数据</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">搜索 Redis 数据</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">通过关键字，数据类型，大小来排序 Redis 数据</p></li><li style="margin: 0px; padding: 0px;"><p style="margin: 0px; padding: 0px; display: inline;">导航历史</p></li></ul></ol><p style="margin: 0px 0px 15pt; padding: 0px; font-family: Verdana, sans-serif, 宋体; line-height: 22.399999618530273px; background-color: #ffffff;">RedisClient 是 &nbsp;<a target="_blank" href="http://www.oschina.net/p/redis" style="margin: 0px; padding: 0px; color: #3e62a6; outline: 0px;">Redis</a>&nbsp;客户端 GUI 工具，使用 Java swt 和jedis 编写。它将redis数据以windows资源管理器的界面风格呈现给用户，可以帮助redis开发人员和维护人员方便的建立，修改，删除，查询redis数据，可以让用户方便的编辑数据，可以剪切，拷贝，粘贴redis数据，可以导入，导出redis数据，可以对redis数据排序。</p><p style="margin: 0px 0px 15pt; padding: 0px; font-family: Verdana, sans-serif, 宋体; line-height: 22.399999618530273px; background-color: #ffffff;"><img src="http://static.oschina.net/uploads/space/2014/0704/180719_ztkY_871298.png" alt="" style="margin: 0px; padding: 0px; border: 0px; max-width: 640px;" /></p><div><a href="https://github.com/caoxinyu/RedisClient" target="_blank">https://github.com/caoxinyu/RedisClient</a></div><img src ="http://www.blogjava.net/paulwong/aggbug/415472.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2014-07-04 21:47 <a href="http://www.blogjava.net/paulwong/archive/2014/07/04/415472.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Redis与Memcached的区别</title><link>http://www.blogjava.net/paulwong/archive/2014/05/23/414020.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Fri, 23 May 2014 04:38:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2014/05/23/414020.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/414020.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2014/05/23/414020.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/414020.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/414020.html</trackback:ping><description><![CDATA[<p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: 微软雅黑, Verdana, sans-serif, 宋体; background-color: #ffffff;"><strong style="padding: 0px; margin: 0px;">传统MySQL+ Memcached架构遇到的问题</strong></p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　实际MySQL是适合进行海量数据存储的，通过Memcached将热点数据加载到cache，加速访问，很多公司都曾经使用过这样的架构，但随着业务数据量的不断增加，和访问量的持续增长，我们遇到了很多问题：</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　1.MySQL需要不断进行拆库拆表，Memcached也需不断跟着扩容，扩容和维护工作占据大量开发时间。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　2.Memcached与MySQL数据库数据一致性问题。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　3.Memcached数据命中率低或down机，大量访问直接穿透到DB，MySQL无法支撑。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　4.跨机房cache同步问题。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;"><strong style="padding: 0px; margin: 0px;">　　众多NoSQL百花齐放，如何选择</strong></p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　最近几年，业界不断涌现出很多各种各样的NoSQL产品，那么如何才能正确地使用好这些产品，最大化地发挥其长处，是我们需要深入研究和思考的问题，实 际归根结底最重要的是了解这些产品的定位，并且了解到每款产品的tradeoffs，在实际应用中做到扬长避短，总体上这些NoSQL主要用于解决以下几 种问题</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　1.少量数据存储，高速读写访问。此类产品通过数据全部in-momery 的方式来保证高速访问，同时提供数据落地的功能，实际这正是Redis最主要的适用场景。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　2.海量数据存储，分布式系统支持，数据一致性保证，方便的集群节点添加/删除。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　3.这方面最具代表性的是dynamo和bigtable 2篇论文所阐述的思路。前者是一个完全无中心的设计，节点之间通过gossip方式传递集群信息，数据保证最终一致性，后者是一个中心化的方案设计，通过类似一个分布式锁服务来保证强一致性,数据写入先写<a href="http://product.it168.com/list/b/0205_1.shtml" target="_blank" rel="nofollow" style="padding: 0px; margin: 0px; color: #ff8373; outline: 0px; font-size: 12px;">内存</a>和redo log，然后定期compat归并到磁盘上，将随机写优化为顺序写，提高写入性能。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　4.Schema free，auto-sharding等。比如目前常见的一些文档数据库都是支持schema-free的，直接存储json格式数据，并且支持auto-sharding等功能，比如mongodb。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　面对这些不同类型的NoSQL产品,我们需要根据我们的业务场景选择最合适的产品。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;"><strong style="padding: 0px; margin: 0px;">　　Redis适用场景，如何正确的使用</strong></p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　前面已经分析过，Redis最适合所有数据in-momory的场景，虽然Redis也提供持久化功能，但实际更多的是一个disk-backed的功 能，跟传统意义上的持久化有比较大的差别，那么可能大家就会有疑问，似乎Redis更像一个加强版的Memcached，那么何时使用 Memcached,何时使用Redis呢?</p><p style="padding: 0px; margin: 8px 0px; line-height: 22.5px; letter-spacing: 0.5px; font-size: 13px; color: #333333; font-family: 微软雅黑, Verdana, sans-serif, 宋体; background-color: #ffffff;">&nbsp;</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">如果简单地比较Redis与Memcached的区别，大多数都会得到以下观点：<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />1&nbsp; Redis不仅仅支持简单的k/v类型的数据，同时还提供list，set，zset，hash等数据结构的存储。<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />2&nbsp; Redis支持数据的备份，即master-slave模式的数据备份。<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />3&nbsp; Redis支持数据的持久化，可以将内存中的数据保持在磁盘中，重启的时候可以再次加载进行使用。<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />抛开这些，可以深入到Redis内部构造去观察更加本质的区别，理解Redis的设计。<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />在Redis中，并不是所有的数据都一直存储在内存中的。这是和Memcached相比一个最大的区别。Redis只会缓存所有的 key的信息，如果Redis发现内存的使用量超过了某一个阀值，将触发swap的操作，Redis根据&#8220;swappability = age*log(size_in_memory)&#8221;计 算出哪些key对应的value需要swap到磁盘。然后再将这些key对应的value持久化到磁盘中，同时在内存中清除。这种特性使得Redis可以 保持超过其机器本身内存大小的数据。当然，机器本身的内存必须要能够保持所有的key，毕竟这些数据是不会进行swap操作的。同时由于Redis将内存 中的数据swap到磁盘中的时候，提供服务的主线程和进行swap操作的子线程会共享这部分内存，所以如果更新需要swap的数据，Redis将阻塞这个 操作，直到子线程完成swap操作后才可以进行修改。<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />使用Redis特有内存模型前后的情况对比：<br style="padding: 0px; margin: 0px;" />VM off: 300k keys, 4096 bytes values: 1.3G used<br style="padding: 0px; margin: 0px;" />VM on:&nbsp; 300k keys, 4096 bytes values: 73M used<br style="padding: 0px; margin: 0px;" />VM off: 1 million keys, 256 bytes values: 430.12M used<br style="padding: 0px; margin: 0px;" />VM on:&nbsp; 1 million keys, 256 bytes values: 160.09M used<br style="padding: 0px; margin: 0px;" />VM on:&nbsp; 1 million keys, values as large as you want, still: 160.09M used<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />当 从Redis中读取数据的时候，如果读取的key对应的value不在内存中，那么Redis就需要从swap文件中加载相应数据，然后再返回给请求方。 这里就存在一个I/O线程池的问题。在默认的情况下，Redis会出现阻塞，即完成所有的swap文件加载后才会相应。这种策略在客户端的数量较小，进行 批量操作的时候比较合适。但是如果将Redis应用在一个大型的网站应用程序中，这显然是无法满足大并发的情况的。所以Redis运行我们设置I/O线程 池的大小，对需要从swap文件中加载相应数据的读取请求进行并发操作，减少阻塞的时间。<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />如果希望在海量数据的环境中使用好Redis，我相信理解Redis的内存设计和阻塞的情况是不可缺少的。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">&nbsp;</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;"><strong style="padding: 0px; margin: 0px;">补充的知识点：</strong></p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">memcached和redis的比较</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">1 网络IO模型</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　Memcached是多线程，非阻塞IO复用的网络模型，分为监听主线程和worker子线程，监听线程监听网络连接，接受请求后，将连接描述字 pipe 传递给worker线程，进行读写IO, 网络层使用libevent封装的事件库，多线程模型可以发挥多核作用，但是引入了cache coherency和锁的问题，比如，Memcached最常用的stats 命令，实际Memcached所有操作都要对这个全局变量加锁，进行计数等工作，带来了性能损耗。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;"><a href="http://static.oschina.net/uploads/img/201405/22095545_Em0P.jpg" target="_blank" style="padding: 0px; margin: 0px; color: #ff8373; outline: 0px; font-size: 12px;"><img alt="" src="http://static.oschina.net/uploads/img/201405/22095545_Em0P.jpg" style="padding: 5px; margin: 10px 0px; border: 1px solid #dddddd; max-width: 640px; background-color: #f4f7f9; cursor: pointer; background-position: initial initial; background-repeat: initial initial;" /></a></p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">(Memcached网络IO模型)</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　Redis使用单线程的IO复用模型，自己封装了一个简单的AeEvent事件处理框架，主要实现了epoll、kqueue和select，对于单纯 只有IO操作来说，单线程可以将速度优势发挥到最大，但是Redis也提供了一些简单的计算功能，比如排序、聚合等，对于这些操作，单线程模型实际会严重 影响整体吞吐量，<a href="http://product.it168.com/list/b/0217_1.shtml" target="_blank" rel="nofollow" style="padding: 0px; margin: 0px; color: #ff8373; outline: 0px; font-size: 12px;">CPU</a>计算过程中，整个IO调度都是被阻塞住的。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　2.<a href="http://product.pcpop.com/Memory/10734_1.html" target="_blank" rel="nofollow" style="padding: 0px; margin: 0px; color: #ff8373; outline: 0px; font-size: 12px;">内存</a>管理方面</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　Memcached使用预分配的内存池的方式，使用slab和大小不同的chunk来管理内存，Item根据大小选择合适的chunk存储，内存池的方 式可以省去申请/释放内存的开销，并且能减小内存碎片产生，但这种方式也会带来一定程度上的空间浪费，并且在内存仍然有很大空间时，新的数据也可能会被剔 除，原因可以参考Timyang的文章：http://timyang.net/data/Memcached-lru-evictions/</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　Redis使用现场申请内存的方式来存储数据，并且很少使用free-list等方式来优化内存分配，会在一定程度上存在内存碎片，Redis跟据存储 命令参数，会把带过期时间的数据单独存放在一起，并把它们称为临时数据，非临时数据是永远不会被剔除的，即便物理内存不够，导致swap也不会剔除任何非 临时数据(但会尝试剔除部分临时数据)，这点上Redis更适合作为存储而不是cache。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　3.数据一致性问题</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　Memcached提供了cas命令，可以保证多个并发访问操作同一份数据的一致性问题。 Redis没有提供cas 命令，并不能保证这点，不过Redis提供了事务的功能，可以保证一串 命令的原子性，中间不会被任何操作打断。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　4.存储方式及其它方面</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　Memcached基本只支持简单的key-value存储，不支持枚举，不支持持久化和复制等功能</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　Redis除key/value之外，还支持list,set,sorted set,hash等众多数据结构，提供了KEYS</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　进行枚举操作，但不能在线上使用，如果需要枚举线上数据，Redis提供了工具可以直接扫描其dump文件，枚举出所有数据，Redis还同时提供了持久化和复制等功能。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　5.关于不同语言的客户端支持</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　在不同语言的客户端方面，Memcached和Redis都有丰富的第三方客户端可供选择，不过因为Memcached发展的时间更久一些，目前看在客 户端支持方面，Memcached的很多客户端更加成熟稳定，而Redis由于其协议本身就比Memcached复杂，加上作者不断增加新的功能等，对应 第三方客户端跟进速度可能会赶不上，有时可能需要自己在第三方客户端基础上做些修改才能更好的使用。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　根据以上比较不难看出，当我们不希望数据被踢出，或者需要除key/value之外的更多数据类型时，或者需要落地功能时，使用Redis比使用Memcached更合适。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;"><strong style="padding: 0px; margin: 0px;">　　关于Redis的一些周边功能</strong></p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　 　Redis除了作为存储之外还提供了一些其它方面的功能，比如聚合计算、pubsub、scripting等，对于此类功能需要了解其实现原理，清楚地 了解到它的局限性后，才能正确的使用，比如pubsub功能，这个实际是没有任何持久化支持的，消费方连接闪断或重连之间过来的消息是会全部丢失的，又比 如聚合计算和scripting等功能受Redis单线程模型所限，是不可能达到很高的吞吐量的，需要谨慎使用。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　总的来说Redis作者是一位非常勤奋的开发者，可以经常看到作者在尝试着各种不同的新鲜想法和思路，针对这些方面的功能就要求我们需要深入了解后再使用。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;"><strong style="padding: 0px; margin: 0px;">　　总结：</strong></p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　1.Redis使用最佳方式是全部数据in-memory。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　2.Redis更多场景是作为Memcached的替代者来使用。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　3.当需要除key/value之外的更多数据类型支持时，使用Redis更合适。</p><p style="padding: 0px; margin: 8px 0px; line-height: 26px; letter-spacing: 0.5px; color: #333333; font-family: Arial; background-color: #ffffff;">　　4.当存储的数据不能被剔除时，使用Redis更合适。</p><img src ="http://www.blogjava.net/paulwong/aggbug/414020.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2014-05-23 12:38 <a href="http://www.blogjava.net/paulwong/archive/2014/05/23/414020.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Redis与Memcached的区别(转)</title><link>http://www.blogjava.net/paulwong/archive/2013/09/06/403746.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Fri, 06 Sep 2013 03:12:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2013/09/06/403746.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/403746.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2013/09/06/403746.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/403746.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/403746.html</trackback:ping><description><![CDATA[&nbsp;如果简单地比较Redis与Memcached的区别，大多数都会得到以下观点：<br />1 Redis不仅仅支持简单的k/v类型的数据，同时还提供list，set，hash等数据结构的存储。<br />2 Redis支持数据的备份，即master-slave模式的数据备份。<br />3 Redis支持数据的持久化，可以将内存中的数据保持在磁盘中，重启的时候可以再次加载进行使用。<br /><br />在Redis中，并不是所有的数据都一直存储在内存中的。这是和Memcached相比一个最大的区别（我个人是这么认为的）。<br /><br />Redis只会缓存所有的key的信息，如果Redis发现内存的使用量超过了某一个阀值，将触发swap的操作，Redis根据&#8220;swappability = age*log(size_in_memory)&#8221;计算出哪些key对应的value需要swap到磁盘。然后再将这些key对应的value持久化到磁盘中，同时在内存中清除。这种特性使得Redis可以保持超过其机器本身内存大小的数据。当然，机器本身的内存必须要能够保持所有的key，毕竟这些数据是不会进行swap操作的。<br /><br />同时由于Redis将内存中的数据swap到磁盘中的时候，提供服务的主线程和进行swap操作的子线程会共享这部分内存，所以如果更新需要swap的数据，Redis将阻塞这个操作，直到子线程完成swap操作后才可以进行修改。<br /><br />可以参考使用Redis特有内存模型前后的情况对比：<br /> <br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->VM&nbsp;off:&nbsp;300k&nbsp;keys,&nbsp;4096&nbsp;bytes&nbsp;values:&nbsp;1.3G&nbsp;used<br />VM&nbsp;on:&nbsp;300k&nbsp;keys,&nbsp;4096&nbsp;bytes&nbsp;values:&nbsp;73M&nbsp;used<br />VM&nbsp;off:&nbsp;1&nbsp;million&nbsp;keys,&nbsp;256&nbsp;bytes&nbsp;values:&nbsp;430.12M&nbsp;used<br />VM&nbsp;on:&nbsp;1&nbsp;million&nbsp;keys,&nbsp;256&nbsp;bytes&nbsp;values:&nbsp;160.09M&nbsp;used<br />VM&nbsp;on:&nbsp;1&nbsp;million&nbsp;keys,&nbsp;values&nbsp;as&nbsp;large&nbsp;as&nbsp;you&nbsp;want,&nbsp;still:&nbsp;160.09M&nbsp;used&nbsp;</div> <br />当从Redis中读取数据的时候，如果读取的key对应的value不在内存中，那么Redis就需要从swap文件中加载相应数据，然后再返回给请求方。这里就存在一个I/O线程池的问题。在默认的情况下，Redis会出现阻塞，即完成所有的swap文件加载后才会相应。这种策略在客户端的数量较小，进行批量操作的时候比较合适。但是如果将Redis应用在一个大型的网站应用程序中，这显然是无法满足大并发的情况的。所以Redis运行我们设置I/O线程池的大小，对需要从swap文件中加载相应数据的读取请求进行并发操作，减少阻塞的时间。<br /> <br />redis、memcache、mongoDB 对比<br />从以下几个维度，对redis、memcache、mongoDB 做了对比，欢迎拍砖<br /><br />1、性能<br />都比较高，性能对我们来说应该都不是瓶颈<br />总体来讲，TPS方面redis和memcache差不多，要大于mongodb<br /><br /><br />2、操作的便利性<br />memcache数据结构单一<br />redis丰富一些，数据操作方面，redis更好一些，较少的网络IO次数<br />mongodb支持丰富的数据表达，索引，最类似关系型数据库，支持的查询语言非常丰富<br /><br /><br />3、内存空间的大小和数据量的大小<br />redis在2.0版本后增加了自己的VM特性，突破物理内存的限制；可以对key value设置过期时间（类似memcache）<br />memcache可以修改最大可用内存,采用LRU算法<br />mongoDB适合大数据量的存储，依赖操作系统VM做内存管理，吃内存也比较厉害，服务不要和别的服务在一起<br /><br />4、可用性（单点问题）<br /><br />对于单点问题，<br />redis，依赖客户端来实现分布式读写；主从复制时，每次从节点重新连接主节点都要依赖整个快照,无增量复制，因性能和效率问题，<br />所以单点问题比较复杂；不支持自动sharding,需要依赖程序设定一致hash 机制。<br />一种替代方案是，不用redis本身的复制机制，采用自己做主动复制（多份存储），或者改成增量复制的方式（需要自己实现），一致性问题和性能的权衡<br /><br />Memcache本身没有数据冗余机制，也没必要；对于故障预防，采用依赖成熟的hash或者环状的算法，解决单点故障引起的抖动问题。<br /><br />mongoDB支持master-slave,replicaset（内部采用paxos选举算法，自动故障恢复）,auto sharding机制，对客户端屏蔽了故障转移和切分机制。<br /><br /><br />5、可靠性（持久化）<br /><br />对于数据持久化和数据恢复，<br /><br />redis支持（快照、AOF）：依赖快照进行持久化，aof增强了可靠性的同时，对性能有所影响<br /><br />memcache不支持，通常用在做缓存,提升性能；<br /><br />MongoDB从1.8版本开始采用binlog方式支持持久化的可靠性<br /><br /><br />6、数据一致性（事务支持）<br /><br />Memcache 在并发场景下，用cas保证一致性<br /><br />redis事务支持比较弱，只能保证事务中的每个操作连续执行<br /><br />mongoDB不支持事务<br /><br /><br />7、数据分析<br /><br />mongoDB内置了数据分析的功能(mapreduce),其他不支持<br /><br /><br />8、应用场景<br />redis：数据量较小的更性能操作和运算上<br /><br />memcache：用于在动态系统中减少数据库负载，提升性能;做缓存，提高性能（适合读多写少，对于数据量比较大，可以采用sharding）<br /><br />MongoDB:主要解决海量数据的访问效率问题<img src ="http://www.blogjava.net/paulwong/aggbug/403746.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2013-09-06 11:12 <a href="http://www.blogjava.net/paulwong/archive/2013/09/06/403746.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>REDIS资源</title><link>http://www.blogjava.net/paulwong/archive/2013/09/01/403533.html</link><dc:creator>paulwong</dc:creator><author>paulwong</author><pubDate>Sun, 01 Sep 2013 05:21:00 GMT</pubDate><guid>http://www.blogjava.net/paulwong/archive/2013/09/01/403533.html</guid><wfw:comment>http://www.blogjava.net/paulwong/comments/403533.html</wfw:comment><comments>http://www.blogjava.net/paulwong/archive/2013/09/01/403533.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/paulwong/comments/commentRss/403533.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/paulwong/services/trackbacks/403533.html</trackback:ping><description><![CDATA[R利剑NoSQL系列文章 之 MongoDB<br /><a href="http://cos.name/2013/04/nosql-r-redis/" target="_blank">http://cos.name/2013/04/nosql-r-redis/</a><br /><br />REDIS书籍<br /><a href="http://abcfy2.gitbooks.io/redis-in-action-reading-notes/getting_to_know_redis/session1.html" target="_blank">http://abcfy2.gitbooks.io/redis-in-action-reading-notes/getting_to_know_redis/session1.html</a><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.blogjava.net/paulwong/aggbug/403533.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/paulwong/" target="_blank">paulwong</a> 2013-09-01 13:21 <a href="http://www.blogjava.net/paulwong/archive/2013/09/01/403533.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>