我一直在努力用PHP来理解和编写Memcache,但我在几点上感到困惑.我已经阅读了很多文章,几乎所有与此相关的SO问题都找不到确切的答案.
1)在PHP中创建Consistent Hashed Key的代码是什么?我必须安装哪些库以及我真正需要做什么?有什么好文章要经历吗?
2)假设,我已经成功存储了一致的哈希密钥,现在如果我的任何服务器关闭或添加了新的服务器,即使我使用的是一致的哈希密钥等,它会有什么不同吗?
3)如果在http://ru.php.net/manual/en/memcached.addserver.php中说明,使用Memcached :: addServers()而不是Memcached :: addServer()会对Consistent Hashing产生任何影响.那不是什么意思?
$m = new Memcached();
$m->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$m->addServers($servers);
Run Code Online (Sandbox Code Playgroud)
4)使用上面的代码是否足以进行Consistent Hashing,然后添加/删除服务器对密钥没有任何影响?
5)什么是Ketama图书馆?如果Memcached :: DISTRIBUTION_CONSISTENT可以更好地工作,为什么要使用它?关注http://www.last.fm/user/RJ/journal/2007/04/10/rz_libketama_-_a_consistent_hashing_algo_for_memcache_clients
6)我是否必须以某种方式哈希我的密钥或只提供我的密钥并让Memcached处理剩下的事情?
请大家,我需要您真正的支持,以便尽快了解并实施我的生产环境.你的答案让我明白我应该为什么编码更好.
我在apache/php windows 10上有memcache扩展(最后的细节)
创建了一个简单的测试:
$memcache = new Memcache;
$memcache->addServer("ext-memcached.e-memcached.xxx.net",11211);
$memcache->addServer("ext-memcached.e-memcached2.xxx.net",11211);
$memcache->addServer("ext-memcached.e-memcached3.xxx.net",11211);
$key='xxx_54921';
$settings = $memcache->get($key);
print_r ($settings);
Run Code Online (Sandbox Code Playgroud)
memcache服务器在AWS上运行良好(生产服务器).此测试代码有效 - 它从内存缓存服务器中检索值.但是,如果我等待几分钟并刷新它将不会返回值.然后如果我再次刷新它将返回值.
相同的代码/配置适用于另一台开发计算机.
是什么导致这个?
Config:
PHP Version 5.6.34
Windows NT SPECTRE 6.2 build 9200 (Windows 8 Home Premium Edition) i586
Build Date Feb 28 2018 17:45:55
Compiler MSVC11 (Visual C++ 2012)
Architecture x86
Memcache extension:
ts x86 version from here:
https://windows.php.net/downloads/pecl/releases/memcache/3.0.8/
memcache info:
memcache support enabled
Version 3.0.8
Revision $Revision: 329835 $
Directive Local Value Master Value
memcache.allow_failover 1 1
memcache.chunk_size 32768 …Run Code Online (Sandbox Code Playgroud) 使用Amazon Linux在AWS上使用php 5.4运行应用程序.
PHP版本是PHP 5.4.28.从AWS repo安装的memcache lib是php54-pecl-memcache-3.0.8-1.11.amzn1.x86_64
我已经验证php正在使用/etc/php.ini:
[root@ip-xx.xx.xx.xx]# php -i | grep Config
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini
Run Code Online (Sandbox Code Playgroud)
设置显示我应该使用memcache:
[root@ip-10-40-17-119 etc]# grep "^session.save" php.ini
session.save_handler="memcache"
session.save_path="tcp://<elasticache-endpoint>:11211"
[root@ip-10-40-17-119 php.d]# php -i | grep session.save
session.save_handler => memcache => memcache
session.save_path => tcp://<elasicache-endpoint>:11211?persistent=1&weight=1&timeout=1&retry_interval=15 => tcp://<elasticache-endpoint>:11211?persistent=1&weight=1&timeout=1&retry_interval=15
Run Code Online (Sandbox Code Playgroud)
我可以从框中telnet到端点和端口并正确连接,因此实例可以连接到memcached服务器.
我们尝试过的事情:
无论我们尝试过什么,会话都存储在磁盘上/ var/lib/php/sessions中.是否有我遗漏的东西,或者这是一个已知的5.4或AWS问题?
我有以下代码:
$cluster['local'] = array('host' => '192.168.1.1', 'port' => '11211', 'weight' => 50);
$cluster['local2'] = array('host' => '192.168.1.2', 'port' => '11211', 'weight' => 50);
$this->memcache = new Memcache;
foreach ($this->cluster() as $cluster) {
$this->memcache->addServer($cluster['host'], $cluster['port'], $this->persistent, $cluster['weight'], 10, 10, TRUE , 'failure' );
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数来检查我的memcache池中的任何服务器是否可用.怎么可以这样做?
我在主域上有一个遗留的 PHP 应用程序,它使用 memcache 作为我们会话的后端。这很好用。我正在尝试在子域上添加一个新的 Zend Framework 应用程序,该应用程序可以共享在主域上创建的相同会话。
我在 application.ini 中配置了会话和保存处理程序:
resources.session.saveHandler.class = "App_Session_SaveHandler_Cache"
resources.session.saveHandler.options.type = "Libmemcached"
resources.session.saveHandler.options.maxlifetime = 300
resources.session.saveHandler.options.front.lifetime = 300
resources.session.saveHandler.options.front.automatic_serialization = true
resources.session.saveHandler.options.back.servers.0.host = "127.0.0.1"
resources.session.saveHandler.options.back.servers.0.port = 11211
resources.session.saveHandler.options.back.servers.0.persistent = true
resources.session.saveHandler.options.back.servers.0.status = true
resources.session.saveHandler.options.back.servers.1.host = "127.0.0.2"
resources.session.saveHandler.options.back.servers.1.port = 11211
resources.session.saveHandler.options.back.servers.1.persistent = true
resources.session.saveHandler.options.back.servers.1.status = true
Run Code Online (Sandbox Code Playgroud)
会话保存处理程序类(App_Session_SaveHanlder_Cache 实现 Zend_Session_SaveHandler_Interface 并使用 Zend_Cache 工厂传递后端存储的 Libmemcached 选项。
在我的引导程序中,我将 _init 方法列为调用会话引导程序的第一个方法:
protected function _initForceSession()
{
$this->bootstrap('session');
Zend_Session::start();
}
Run Code Online (Sandbox Code Playgroud)
我希望能够继续使用来自我的子域中主域的现有会话(如果它存在)并创建一个新会话(如果那里还没有)。
我能够在旧应用程序中创建的新应用程序中成功读取会话 ID。但是,我无法让新应用程序在会话本身中实际读取。
我尝试从新应用程序直接读取内存缓存(绕过会话),我能够以这种方式获取会话数据。
我在这里缺少什么?
刚刚得到一台新的笔记本电脑,从头开始设置我的开发环境,我得到这个奇怪的错误似乎不是缺少依赖关系的结果(虽然我可能是错的).我正在使用Python 2.7.3开发virtualenv.Libevent,libmemcached和memcached都是早先用Homebrew安装的.
(grlli)Tylers-MacBook-Pro:grlli tyler$ pip install pylibmc
Downloading/unpacking pylibmc
Running setup.py egg_info for package pylibmc
Installing collected packages: pylibmc
Running setup.py install for pylibmc
building '_pylibmc' extension
cc -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -I/usr/local/include -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -DUSE_ZLIB -I/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _pylibmcmodule.c -o build/temp.macosx-10.8-intel-2.7/_pylibmcmodule.o -fno-strict-aliasing
_pylibmcmodule.c:1895:33: error: member reference base type 'const void' is not a structure or union
svr->hostname, svr->port);
~~~~~^~~~~~~~~~~~~~~~~~~~
/usr/include/secure/_stdio.h:56:62: note: expanded from macro 'snprintf'
__builtin___snprintf_chk (str, len, 0, __darwin_obsz(str), __VA_ARGS__)
^
1 error …Run Code Online (Sandbox Code Playgroud) 我正在尝试pecl install memcache在 OS X Yosemite 上使用 XAMPP执行 a ,但找不到zlib,产生此错误:
检查 ZLIB 的位置...没有
检查 zlib 的位置...配置:错误:内存缓存支持需要 ZLIB。
使用 --with-zlib-dir= 指定 ZLIB 包含和库所在的前缀
错误:`/private/tmp/pear/temp/memcache/configure --enable-memcache-session=yes' failed
如何我安装了zlib使这pecl install memcache能成功吗?
我开始在我的应用程序中添加一个memcached层.到现在为止还挺好.
我很快意识到当有人将文件上传到网站以保持数据相关时,我将需要一次性无效/删除大批量的密钥.
我已经完成了一些阅读,解决这个问题的最常见方法似乎是在每个密钥上设置一个版本号,然后在用户上传时删除密钥(因为可能有这么多的排列)你会增加版本号,在下次访问数据时引起高速缓存未命中.
我不知道从哪里开始才能得到这个编码,我不太确定我是否完全了解它.
我的代码目前看起来像这样: -
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$key = md5("songsearch_$textSearch.$rangeSearch");
Run Code Online (Sandbox Code Playgroud)
上面的key var中的两个变量是从get请求中检索的,而get请求又会检索大量的JSON.(想想产品目录).
这些变量还确定了查询本身,它根据这些变量动态组合在一起.最终所有这些都为我提供了每个查询所特有的密钥,即使从同一个脚本中我也可以生成数百个密钥.
我希望以上内容是清楚的,如果没有,请让我澄清任何要点,以便更好地帮助你回答我的问题.
显然稍后,设置缓存我使用的结果: -
$memcache->set($key, $output, MEMCACHE_COMPRESSED, time() + 24*60*60*365);
Run Code Online (Sandbox Code Playgroud)
就在我编码JSON之前.
所以我的问题确实是......我如何为此添加某种形式的增量版本,这样如果用户上传文件,我可以使这个脚本生成的所有密钥无效?
非常感谢任何让我至少走上正轨的人.
我正在尝试在运行10.9(Mavericks)的Mac上在MAMP中安装PECL memcache扩展.我刚升级到MAMP 3.0,需要重新安装.
我sudo pecl install memcache在终端跑了但是我的路径似乎塞满了它找不到php.h.它似乎在寻找/Applications/MAMP/bin/php/php5.5.10/include/php/main/php.h.将/Applications/MAMP/bin/php/php5.5.10/include/不存在.
这是我的终端日志.其他人遇到过这个问题吗?
$ sudo pecl install memcache
downloading memcache-2.2.7.tgz ...
Starting to download memcache-2.2.7.tgz (36,459 bytes)
..........done: 36,459 bytes
11 source files, building
WARNING: php_bin /Applications/MAMP/bin/php/php5.5.10/bin/php appears to have a suffix /php5.5.10/bin/php, but config variable php_suffix does not match
running: phpize
grep: /Applications/MAMP/bin/php/php5.5.10/include/php/main/php.h: No such file or directory
grep: /Applications/MAMP/bin/php/php5.5.10/include/php/Zend/zend_modules.h: No such file or directory
grep: /Applications/MAMP/bin/php/php5.5.10/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api …Run Code Online (Sandbox Code Playgroud)