一 理论补充

  缓存是临时开辟出来的一块存放数据的区域, 这样在下一次取数据的时候就可以直接去取了.

  因为内存的材质相对于硬盘的读写明显优越性,而且SDRAM等的性价比高,缓存最好保存在内存里.PHP中的缓存主要分两种,逻辑代码层面的缓存和数据层面的缓存.逻辑缓存主要是编译缓存,php 是脚本解析形式的语言,即php 是不转换为机器码的,php在服务器cgi 分配给进程,fastcgi是常住内存的进程,fpm管理cgi的进程调度,有几种IO模式例如Epoll,worker 等.php代码首先被编译成zend 虚拟机的opcode 二进制码,然后被zvm执行,opcode 是进程创建在内存中的,进程之间不共享,为共享需要专门开辟出一个空间,是用专门的程序管理,如APC||xcache||zend optimizer等;数据缓存比较多而且常见,如redis,memcache,ob,smarty等对php的结果进行缓存.

二 缓存选用

  因为领导点名要用memcache 所以先写一个memcache 的.

  1. 安装. Centos6.3 安装memcache,编译安装.  

https://github.com/memcached/memcached/wiki/ReleaseNotes1425

wget || tar zxvf || ./configure --prefix=/src/memcache  --with-libevent=/src/libevent/

***所以需要先安装libevent ,libevent 是一个事件通知机制的类,被用于多种模式下,包括epool|| pool || select 等多种IO机制下.  

http://libevent.org/ 下载, tar zxvf || cd || ./configure --prefix=/src/libevent/  make &&make (test) install && make clean

安装完毕.启动/src/memcache -d -m 1024 -u root  // 启动memcache

*** memcache是一个项目,memcached 是程序文件.存的是hashtable ,用的是LRU,缓存算法很多种,有兴趣的自己看

 1 memcached 1.4.25
 2 -p <num>      TCP port number to listen on (default: 11211)  //tcp端口号
 3 -U <num>      UDP port number to listen on (default: 11211, 0 is off) //udp 端口号
 4 -s <file>     UNIX socket path to listen on (disables network support) //socket 连接,类似于mysql.sock
 5 -A            enable ascii "shutdown" command  
 6 -a <mask>     access mask for UNIX socket, in octal (default: 0700)
 7 -l <addr>     interface to listen on (default: INADDR_ANY, all addresses)   //监听端口
 8               <addr> may be specified as host:port. If you don't specify
 9               a port number, the value you specified with -p or -U is
10               used. You may specify multiple addresses separated by comma
11               or by using -l multiple times
12 -d            run as a daemon //作为一个守护进程
13 -r            maximize core file limit 
14 -u <username> assume identity of <username> (only when run as root)
15 -m <num>      max memory to use for items in megabytes (default: 64 MB)
16 -M            return error on memory exhausted (rather than removing items)
17 -c <num>      max simultaneous connections (default: 1024)
18 -k            lock down all paged memory.  Note that there is a
19               limit on how much memory you may lock.  Trying to
20               allocate more than that would fail, so be sure you
21               set the limit correctly for the user you started
22               the daemon with (not for -u <username> user;
23               under sh this is done with 'ulimit -S -l NUM_KB').
24 -v            verbose (print errors/warnings while in event loop)
25 -vv           very verbose (also print client commands/reponses)
26 -vvv          extremely verbose (also print internal state transitions)
27 -h            print this help and exit
28 -i            print memcached and libevent license
29 -V            print version and exit
30 -P <file>     save PID in <file>, only used with -d option
31 -f <factor>   chunk size growth factor (default: 1.25)
32 -n <bytes>    minimum space allocated for key+value+flags (default: 48)
33 -L            Try to use large memory pages (if available). Increasing
34               the memory page size could reduce the number of TLB misses
35               and improve the performance. In order to get large pages
36               from the OS, memcached will allocate the total item-cache
37               in one large chunk.
38 -D <char>     Use <char> as the delimiter between key prefixes and IDs.
39               This is used for per-prefix stats reporting. The default is
40               ":" (colon). If this option is specified, stats collection
41               is turned on automatically; if not, then it may be turned on
42               by sending the "stats detail on" command to the server.
43 -t <num>      number of threads to use (default: 4)
44 -R            Maximum number of requests per event, limits the number of
45               requests process for a given connection to prevent 
46               starvation (default: 20)
47 -C            Disable use of CAS
48 -b <num>      Set the backlog queue limit (default: 1024)
49 -B            Binding protocol - one of ascii, binary, or auto (default)
50 -I            Override the size of each slab page. Adjusts max item size
51               (default: 1mb, min: 1k, max: 128m)
52 -F            Disable flush_all command
53 -o            Comma separated list of extended or experimental options
54               - (EXPERIMENTAL) maxconns_fast: immediately close new
55                 connections if over maxconns limit
56               - hashpower: An integer multiplier for how large the hash
57                 table should be. Can be grown at runtime if not big enough.
58                 Set this based on "STAT hash_power_level" before a 
59                 restart.
60               - tail_repair_time: Time in seconds that indicates how long to wait before
61                 forcefully taking over the LRU tail item whose refcount has leaked.
62                 Disabled by default; dangerous option.
63               - hash_algorithm: The hash table algorithm
64                 default is jenkins hash. options: jenkins, murmur3
65               - lru_crawler: Enable LRU Crawler background thread
66               - lru_crawler_sleep: Microseconds to sleep between items
67                 default is 100.
68               - lru_crawler_tocrawl: Max items to crawl per slab per run
69                 default is 0 (unlimited)
70               - lru_maintainer: Enable new LRU system + background thread
71               - hot_lru_pct: Pct of slab memory to reserve for hot lru.
72                 (requires lru_maintainer)
73               - warm_lru_pct: Pct of slab memory to reserve for warm lru.
74                 (requires lru_maintainer)
75               - expirezero_does_not_evict: Items set to not expire, will not evict.
76                 (requires lru_maintainer)

启动 memcache  memcache -d -uroot

*** 没有acl ,可以用-l 指定listen地址,也可以用iptables,没有认证机制,没有写文件机制,这些自己可以源码,分布式用代码来保证。

三  试用

  万能的telnet ,win7/8/10在control,安装telnet,xp自带,unix yum install

  telnet 127.0.0.1 11211 本人用的nat模式虚拟机,所以还得配个pat,可以用ssh,也可在vminter网关配.竟然没效果,试下80 

80 有用,开个11211 等下

OK了,回显还是有问题,不影响验证功能. *** 启动memcache时候没有配listen 127.0.0.1 是为了这里的pat

stats 看下

四  基本命令

  set ||add ||replace || delete || stats || flush || get

五 windows 下结合php使用

  下载php_memcache.dll,加入到ext目录,在ini文件中加载

  写一个保存session的小程序

 1 <?php
 2 
 3 $memcache = memcache_connect('localhost', 11211);
 4 
 5 if ($memcache) {
 6     ini_set("session.save_handler", "memcache");
 7     ini_set("session.save_path", "tcp://127.0.0.1:11211");
 8     session_start();
 9     
10     $_SESSION['name']='lyx';
11     $_SESSION['time']=time();
12     echo session_id();
13 }
14 else {
15     echo "Connection to memcached failed";
16 }
17 ?>

读取session

1 <?php
2  
3 $mem = new Memcache(); 
4 $mem->connect("127.0.0.1", 11211); 
5 echo $mem->get('ga4c4g4q54vdkihsug7tk2cb63');

页面显示效果如下

nice!! 写到这里, 诚收指导意见.小三爷谢过~!

 

** memcached 目前只支持linux 上用libevent调用,在代码中可以new memcached这样子.windows 上 new memcache ,memcache 相对少了许多方法,setOption等.

 

  

posted on 2015-12-07 23:38  任城三爷  阅读(168)  评论(0编辑  收藏  举报