514. char *key, *value; 515. int key_l, value_l; 516. double ttl = 0; 517. double set_ct = 0; 518.
519. if (2 != ZEND_NUM_ARGS()) 520. {
521. WRONG_PARAM_COUNT; 522. } 523.
524. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \&key, &key_l, &value, &value_l, ttl) == FAILURE) 525. {
526. RETURN_FALSE; 527. } 528.
529. // Write data into every object 530. apr_int32_t i = 0; 531. if (ttl < 0) 532. {
533. ttl = 0; 534. } 535.
536. apr_status_t rv; 537.
538. for (i = 0; i < MAX_GROUP; i++) 539. {
540. if (0 == is_validate_group(i)) 541. {
542. // Write it!
543. rv = apr_memcache_add(mc_groups[i], key, value, value_l, (apr_uint32_t) ttl, 0); 544. if (APR_SUCCESS == rv) 545. {
546. set_ct++; 547. } 548. } 549. } 550.
551. RETURN_DOUBLE(set_ct); 552. }
553. 在mc_get中,首先要随机选择一个组,然后从这个组开始轮询: 554. /**
555. * Fetch a item from a random group
556. */
557. PHP_FUNCTION(mc_get) 558. {
559. char *key, *value = NULL; 560. int key_l;
561. apr_size_t value_l; 562.
563. if (1 != ZEND_NUM_ARGS()) 564. {
565. WRONG_PARAM_COUNT; 566. } 567.
568. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \&key, &key_l) == FAILURE) 569. {
570. RETURN_MULL(); 571. } 572.
573. // I will try ... 574. // Random read
575. apr_int32_t curr_group_id = random_group(); 576. apr_int32_t i = 0; 577. apr_int32_t try = 0; 578. apr_uint32_t flag; 579. apr_memcache_t *oper; 580. apr_status_t rv; 581.
582. for (i = 0; i < MAX_GROUP; i++) 583. {
584. try = i + curr_group_id; 585. try = try % MAX_GROUP;
586. if (0 == is_validate_group(try)) 587. {
588. // Get a value
589. oper = mc_groups[try];
590. rv = apr_memcache_getp(mc_groups[try], p, (const char *) key, &value, &value_l, 0); 591. if (APR_SUCCESS == rv) 592. {
593. RETURN_STRING(value, 1); 594. } 595. } 596. } 597.
598. RETURN_FALSE; 599. } 600. /**
601. * Random group id 602. * For mc_get() 603. */
604. apr_int32_t random_group() 605. {
606. struct timeval tv; 607. struct timezone tz; 608. int usec; 609.
610. gettimeofday(&tv, &tz); 611.
612. usec = tv.tv_usec; 613.
614. int curr = usec % count_group(); 615.
616. return (apr_int32_t) curr; 617. }
618. BSM_Memcache的使用方式和其它的client类似: 619.
620. $g1 = mc_add_group(); // 添加第一个组 621. $g2 = mc_add_group(); // 添加第二个组
622. mc_add_server($g1, 'localhost:11211'); // 在第一个组中添加第一台服务器
623. mc_add_server($g1, 'localhost:11212'); // 在第一个组中添加第二台服务器
624. mc_add_server($g2, '10.0.0.16:11211'); // 在第二个组中添加第一台服务器
625. mc_add_server($g2, '10.0.0.17:11211'); // 在第二个组中添加第二台服务器 626.
627. mc_set('key', 'Hello'); // 写入数据 628. $key = mc_get('key'); // 读出数据 629. mc_del('key'); // 删除数据 630. mc_shutdown(); // 关闭所有组 631. ?>
632. APR_Memcache的相关资料可以在这里找到,BSM_Memcache可以在网络上找下载。
633. ◎APR环境介绍 634. APR的全称:Apache Portable Runtime。它是Apache软件基金会创建并维持的一套跨平台的C语言库。它从Apache httpd1.x中抽取出来并独立于httpd之外,Apache httpd2.x就是建立在APR上。APR提供了很
多方便的API接口可供使用,包括如内存池、字符串操作、网络、数组、hash表等实用的功能。开发 Apache2 Module要接触很多APR函数,当然APR可以独立安装独立使用,可以用来写自己的应用程序,不一定是Apache httpd的相关开发。 635. ◎后记
636. 这是我在农历丙戌年(我的本命年)的最后一篇文章,由于Memcached的内涵很多,仓促整理一定有很多遗漏和错误。感谢新浪网提供的研究机会,感谢部门同事的帮助。
637. 分布式缓存系统Memcached简介与实践
638. 一 Memcached服务器端的安装 (此处将其作为系统服务安装) 639. 下载文件:memcached 1.2.1 for Win32 binaries (Dec 23, 2006) 640. 1 解压缩文件到c:memcached
641. 2命令行输入 'c:memcachedmemcached.exe -d install' 642. 3 命令行输入 'c:memcachedmemcached.exe -d start',该命令启动 Memcached ,默认监听端口为 11211
643. 通过 memcached.exe -h 可以查看其帮助 644. 二 .NET memcached client library 645. 下载文件:
https://sourceforge.net/projects/memcacheddotnet/
646. 里面有.net1.1 和 .net2.0的两种版本 还有一个不错的例子。
647. 三 应用
648. 1 将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目录 649. 2 引用Memcached.ClientLibrary.dll 650. 3 代码
651. 1namespaceMemcached.MemcachedBench 2{
3 usingSystem;
4 usingSystem.Collections; 5
6 usingMemcached.ClientLibrary; 7
8 publicclassMemcachedBench 9 {
10 [STAThread]
11 publicstaticvoidMain(String[]args) 12 { 13
string[]serverlist={\14
15 //初始化池
16 SockIOPoolpool=SockIOPool.GetInstance();
17 pool.SetServers(serverlist); 18
19 pool.InitConnections=3; 20 pool.MinConnections=3; 21 pool.MaxConnections=5; 22
23 pool.SocketConnectTimeout=1000; 24 pool.SocketTimeout=3000; 25
26 pool.MaintenanceSleep=30; 27 pool.Failover=true; 28
29 pool.Nagle=false; 30 pool.Initialize(); 31
32 //获得客户端实例
33 MemcachedClientmc=newMemcachedClient(); 34 mc.EnableCompression=false; 35
36 Console.WriteLine(\测 试-----------\
37 mc.Set(\//存储数据到缓存服务器,这里将字符串\缓存,key是\38
39 if(mc.KeyExists(\//测试缓存存在key为test的项目
40 {
41 Console.WriteLine(\
42 Console.WriteLine(mc.Get(\//在缓存中获取key为test的项目 43 } 44 else 45 {
46 Console.WriteLine(\47 } 48
49 Console.ReadLine(); 50
51 mc.Delete(\//移除缓存中key为test的项目 52
53 if(mc.KeyExists(\54 {
55 Console.WriteLine(\
56 Console.WriteLine(mc.Get(\