Skip to content

老陈是一个普通的文艺二逼青年

For The Dream

天然的memcache —— /dev/shm

Written by chen

首先让我们认识一下,什么是tmpfs和/dev/shm/?

tmpfs是Linux/Unix系统上的一种基于内存的文件系统。tmpfs可以使用您的内存或swap分区来存储文件。由此可见,tmpfs主要存储暂存的文件。
它有如下2个优势 :
1. 动态文件系统的大小。
2. tmpfs 的另一个主要的好处是它闪电般的速度。
因为典型的 tmpfs 文件系统会完全驻留在内存 RAM 中,读写几乎可以是瞬间的。同时它也有一个缺点 tmpfs 数据在重新启动之后不会保留,因为虚拟内存本质上就是易失的。所以有必要做一些脚本做诸如加载,绑定的操作。

tmpfs不具备持久性,重启后数据不保留,请务必注意!!!

性能测试

分别对磁盘、/dev/shm、memcache进行10W次读、写、读写操作。

1
2
3
4
5
            file          /dev/shm           memcache
write       12.01          0.58              2.22
read        0.41           0.40              2.21
write&read  12.51          1.01              4.54
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$st = microtime(true);
 
$file = __DIR__ . "/file_cache";
 
for($i = 0 ; $i < 100000; $i++ ) {
        file_put_contents( $file, "hello world" );
        file_get_contents( $file );
}
 
$ed = microtime(true);
 
 
echo "file:",  $ed - $st, "\n";
 
 
$st = microtime(true);
 
$file = '/dev/shm/file_cache';
 
for($i = 0 ; $i < 100000; $i++ ) {
        file_put_contents( $file, "hello world" );
        file_get_contents( $file );
 
}
 
$ed = microtime(true);
 
 
echo "/dev/shm:" , $ed - $st, "\n";
 
$st = microtime(true);
 
$mc = new Memcache;
$mc -> connect( '127.0.0.1', 11211 );
for( $i = 0; $i < 100000; $i++ ) {
        $mc -> set( 'key', 'hello world' );
        $mc -> get( 'key' );
}
 
$ed = microtime(true);
 
echo "memcache:" , $ed - $st, "\n";
 
 

PHP · linux, memcache, php

Copyright © 2022 老陈是一个普通的文艺二逼青年. 沪ICP备13044041号-1