|
użytkowników online: 37
|
OPINIE UŻYTKOWNIKÓW
|
Porady zamieszczone tutaj przez Darka są pomocne w wielu chwilach. Wielokrotnie tworząc jakiś złożony serwis korzystam z tych porad. Można by tworzyć samemu te skrypty, ale tak naprawdę czy nie lepiej jest wziąć skrypt z tej strony i zmodyfikowac go dla swoich potrzeb? Wprawdzie możemy taki skrypt napisać sami, ale po co, skoro stracimy czas na coś, co ktoś juz napisał, przetestował i może zagwarantować, że działa poprawnie. Któryś raz z rzędu opłacam abonament i nie raz jeszcze opłacę. Kawał dobrej roboty i ogrom wiedzy w jednym miejscu.
Piotr Karamański Design Studio
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
LXIX. Memcache Functions
Memcache module provides handy procedural and object oriented interface
to memcached, highly effective caching daemon, which was especially
designed to decrease database load in dynamic web applications.
This module doesn't have native support of multiple servers, but you
still can implement it yourself in your application. Establish several
memcached connections, set priority level for each server etc.
More information about memcached can be found at http://www.danga.com/memcached/.
This module uses functions of zlib
to support on-the-fly data compression. Zlib is required to install
this module.
PHP 4.3.3 or newer is required to use the memcache extension.
To rozszerzenie PECL nie
jest dołączane do PHP.
Dodatkowe informacje, takie jak nowe wersje, pliki do
pobrania, pliki źródłowe, informacje o opiekunach czy rejestr zmian, można
znaleźć tutaj:
http://pecl.php.net/package/memcache.
In order to use these functions you must compile PHP with MemCache support
by using the --with-memcache[=DIR] option.
Windows users will enable php_memcache.dll inside
of php.ini in order to use these functions.
DLL z tym rozszerzeniem
PECL można pobrać ze strony
PHP Downloads lub
http://snaps.php.net/.
Tabela 1. MemCache Constants | Name | Description |
|---|
|
MEMCACHE_COMPRESSED
(integer)
|
Used to turn on-the-fly data compression on with
Memcache::set(),
Memcache::add() i
Memcache::replace().
|
To rozszerzenie nie definiuje posiada żadnych
dyrektyw konfiguracyjnych w pliku php.ini.
There is only one resource type used in memcache module - it's
the link identifier for a cache server connection.
Przykład 1. memcache extension overview example |
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>
|
|
In the above example, an object is being saved in the cache and then
retrieved back. Object and other non-scalar types are serialized before
saving, so it's impossible to store resources (i.e. connection identifiers
and others) in the cache.
User Contributed Notesiliya at pisem dot net
19-Jan-2006 09:35
one more "intelligent" cache aggregator:
https://svn.shadanakar.org/onPHP/ trunk/core/Cache/AggregateCache.class.php
can be used with several cache connectors - memcached, filesystem, etc.
(remove whitespace manually)
Gregor J. Rothfuss
21-Nov-2005 06:18
Ron
14-Sep-2005 10:19
An improvement to the above:
The above class will cause an error if all cache servers are down. The preferred behavior is to just have a cache miss (or take no action in the case of write operations) and return false, so the app can run in non-cached mode if all cache servers are down.
To make this happen, simply change the connection usage to look something like this in each affected function. This code is for the get() function:
$con = $this->_getConForKey($key);
if ($con === false) return false;
return $con->get($key);
Similarly, the affected code in the set() function would look like this:
$con = $this->_getConForKey($key);
if ($con === false) return false;
return $con->set($key, $var, $compress, $expire);
Modify each function accordingly, and if all of your cache servers are down, you can still function (although more slowly due to the 100% cache miss rate).
Ron
14-Sep-2005 07:20
Here is a simple memcached aggregator class which distributes the cache among multiple cache servers. If a server fails, the load is redistributed automatically. It uses persistent connections.
The constructor takes an array of arrays, with each inner array representing a server, with a 'server' (string) attribute that is the IP addres or host name of the memcached server, and a 'port' (int) attribute that is the port number on which memcached is running on the server.
All of the existing memcached API functions are implemented except getStats() and getVersion(), which are server-specific.
<?php
class MemcachedAggregator {
var $connections;
public function __construct($servers) {
$this->connections = array();
for ($i = 0, $n = count($servers); $i < $n; $i++) {
$server = $servers[$i];
$con = memcache_pconnect($server['host'], $server['port']);
if (!($con == false)) {
$this->connections[] = $con;
}
}
}
private function _getConForKey($key) {
$hashCode = 0;
for ($i = 0, $len = strlen($key); $i < $len; $i++) {
$hashCode = (int)(($hashCode*33)+ord($key[$i])) & 0x7fffffff;
}
if (($ns = count($this->connections)) > 0) {
return $this->connections[$hashCode%$ns];
}
return false;
}
public function debug($on_off) {
$result = false;
for ($i = 0; $i < count($connections); $i++) {
if ($this->connections[$i]->debug($on_off)) $result = true;
}
return $result;
}
public function flush() {
$result = false;
for ($i = 0; $i < count($connections); $i++) {
if ($this->connections[$i]->flush()) $result = true;
}
return $result;
}
public function get($key) {
if (is_array($key)) {
$dest = array();
foreach ($key as $subkey) {
$val = get($subkey);
if (!($val === false)) $dest[$subkey] = $val;
}
return $dest;
} else {
return $this->_getConForKey($key)->get($key);
}
}
public function set($key, $var, $compress=0, $expire=0) {
return $this->_getConForKey($key)->set($key, $var, $compress, $expire);
}
public function add($key, $var, $compress=0, $expire=0) {
return $this->_getConForKey($key)->add($key, $var, $compress, $expire);
}
public function replace($key, $var, $compress=0, $expire=0) {
return $this->_getConForKey($key)->replace
($key, $var, $compress, $expire);
}
public function delete($key, $timeout=0) {
return $this->_getConForKey($key)->delete($key, $timeout);
}
public function increment($key, $value=1) {
return $this->_getConForKey($key)->increment($key, $value);
}
public function decrement($key, $value=1) {
return $this->_getConForKey($key)->decrement($key, $value);
}
}
?>
|