试验场景一:要对同一个list实时添加元素,且放入缓存中。代码如下:
class="java">
@Test
public void testMemCacheAndRedis() {
this.testInsert(1000);
}
public void testInsert(int size) {
MemCached memCached = (MemCached) ctx.getBean("configMemCache");
memCached.flushAll();
long before = System.currentTimeMillis();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
list.add(i);
memCached.put("test", list);
}
long end = System.currentTimeMillis();
RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("bubbleRedisTemplate");
redisTemplate.delete("test123");
long before2 = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
redisTemplate.boundListOps("test123").leftPush(i);
}
long end2 = System.currentTimeMillis();
System.out.println("memcached use time is " + (end - before));
System.out.println("redis use time is " + (end2 - before2));
}
当n=1000时的任意3次打印结果:
=============================================
memcached use time is 6282
redis use time is 1156
memcached use time is 6547
redis use time is 1172
memcached use time is 6234
redis use time is 1375
当n=2000时的任意3次打印结果:
=============================================
memcached use time is 22313
redis use time is 2328
memcached use time is 22969
redis use time is 2907
memcached use time is 22938
redis use time is 2234
当n=3000时的任意3次打印结果:
=============================================
memcached use time is 48859
redis use time is 3297
memcached use time is 48156
redis use time is 4547
memcached use time is 47765
redis use time is 3375
试验一结论(只对本次试验负责):当n在20条以内时,两者差异不大,但当n大于20时,redis在性能上的优势就会逐渐体现出来。
试验场景二:对程序做些稍许的改动,即将memCached.put("xwq", list);这一行移到for循环外面执行,代码如下:
@Test
public void testMemCacheAndRedis() {
this.testBatchInsert(3000);
}
public void testBatchInsert(int size) {
MemCached memCached = (MemCached) ctx.getBean("configMemCache");
memCached.flushAll();
long before = System.currentTimeMillis();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
list.add(i);
}
memCached.put("test", list);
long end = System.currentTimeMillis();
RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("bubbleRedisTemplate");
redisTemplate.delete("test123");
long before2 = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
redisTemplate.boundListOps("test123").leftPush(i);
}
long end2 = System.currentTimeMillis();
System.out.println("memcached use time is " + (end - before));
System.out.println("redis use time is " + (end2 - before2));
}
我们再做下试验:
当n=1000时的任意3次打印结果:
===================================================
memcached use time is 15
redis use time is 1188
memcached use time is 32
redis use time is 1188
memcached use time is 31
redis use time is 1234
当n=2000时的任意3次打印结果:
===================================================
memcached use time is 47
redis use time is 2437
memcached use time is 31
redis use time is 2344
memcached use time is 47
redis use time is 2265
但n=3000时的任意3次打印结果:
===================================================
memcached use time is 62
redis use time is 3406
memcached use time is 47
redis use time is 3391
memcached use time is 47
redis use time is 3688
试验二结论(只对本次试验负责):由此可以看出,memcached一次性将list放入到缓存中,比redis每次更新性能是要高很多的。
试验场景三:对同样大小的两个list,同时进行相同次数的读操作,代码如下:
@Test
public void testMemCacheAndRedis() {
// this.testInsert(3000);
// this.testBatchInsert(3000);
this.testRead(200);
}
public void testRead(int size) {
MemCached memCached = (MemCached) ctx.getBean("configMemCache");
memCached.flushAll();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
list.add(i);
}
memCached.put("test", list);
long before = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
memCached.get("test");
}
long end = System.currentTimeMillis();
RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) ctx.getBean("bubbleRedisTemplate");
redisTemplate.delete("test123");
for (int i = 0; i < 1000; i++) {
redisTemplate.boundListOps("test123").leftPush(i);
}
long before2 = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
redisTemplate.boundListOps("xwq123").range(0, -1);
}
long end2 = System.currentTimeMillis();
System.out.println("memcached use time is " + (end - before));
System.out.println("redis use time is " + (end2 - before2));
}
当n=100时的任意3次打印结果:
============================================
memcached use time is 6765
redis use time is 281
memcached use time is 2187
redis use time is 297
memcached use time is 9547
redis use time is 313
当n=200时的任意3次打印结果:
============================================
memcached use time is 34250
redis use time is 546
memcached use time is 12171
redis use time is 547
memcached use time is 8297
redis use time is 516
当n=500时的任意3次打印结果:
============================================
memcached use time is 69437
redis use time is 1297
memcached use time is 43781
redis use time is 1516
memcached use time is 37750
redis use time is 1281
试验三结论(只对本次试验负责):同样是从缓存中读取数据,redis的性能是要远远高于memcached。
综上所述结论(只对本次次试验负责):通过上述三次试验,redis在读操作和写操作上是全面领先memcached的。但有一点,memcached可以一次性将集合的数据放入自己的缓存中,而redis却不行(有还是木有?),所以memcached并不是一无是处的,在很多场景下,二者是需要交叉使用的,要根据实际场景进行取舍。