• No se han encontrado resultados

LABIOS, BOCA, LENGUA

In document CONDICIONES GENERALES (página 65-87)

En cumplimiento a lo dispuesto en el artículo 202 de la Ley de Instituciones de Seguros y de Fianzas, la Documentación

LABIOS, BOCA, LENGUA

We have a couple of options available to us when looking for an in-memory object cache. Each one is useful and excels for different needs, so let’s talk about each of them.

APC

In the Application Server chapter, we discussed using thePECL APC Extension⁶⁹as a mechanism for speeding up your application by caching the compiled bytecode of your source. On top of that, APC also has an interface for storing and retrieving⁷⁰ data between PHP requests using shared memory, which can be used as an object cache.

Here’s an example of how we can use APC. Imagine that in one script, you have this code, which you run.

1 <?php

2 $array = array("foo1", "foo2", "foo3"); 3 apc_add("foobar", $array);

And in separate script, that runs independently from the code above:

⁶⁹http://pecl.php.net/package/APC

Cache Server: Using Redis 86 1 <?php

2 // $array will contain array("foo1", "foo2", "foo3") after

3 // fetching it from APC.

4 $array = apc_fetch("foobar");

Usingapc_addandapc_fetch, you’re able to store and retrieve data lasts longer than the lifecycle of a normal PHP request. Of course, there are no guarantees and data can come and go, so it should only be used for re-calculatable cache data.

APC offers atomic counters and expiration times, too, which gives it a similar feature set as Memcached.

The biggest pro of using APC is that it’s extremely fast. In fact, it’s faster (at a micro level) than Redis or Memcached. Why? It uses shared memory within the PHP process, so it doesn’t need to go out on the network or communicate with a separate server. It’s biggest asset, however, is also it’s biggest weakness. Because the shared memory is tied to a parent PHP-FPM process, each server has to maintain it’s own separate cache data, effectively making it non-distributed since all cache data needs to be re-calculated and re-stored for each server.

Furthermore, because APC relies on shared memory, it only works withPHP-FPMormod_php. A PHP script running on the command line or in a cronjob won’t be able to see the shared memory because it doesn’t share the same parent process as yourPHP-FPMworkers.

Does this make APC useless? Not at all. It’s highly useful for caching small pieces of data that don’t take too long to compute. For example, I’ve used it with some success for caching configuration files. Without APC, the configuration files (stored inYAML format⁷¹) would have to be re-read and parsed on every request. Instead, since the files don’t change that often, I was able to parse the YAML files once and store the result in APC. The code looked something like this: 1 <?php 2 function load_config() { 3 $config = apc_fetch("config"); 4 5 if ($config === false) { 6 $config = yaml_parse_file("app/config/config.yml");

7 // Store the parsed file in APC for the next access.

8 // The 3rd parameter is an automatic expiration time.

9 apc_add("config", $config, 86400); 10 }

11

12 return $config; 13 }

It’s worth noting that the APC cache is never persisted to disk. That means if PHP-FPMcrashes or gets restarted, the cache will be wiped clean.

Cache Server: Using Redis 87

Memcached

Memcached is the ol’reliable object cache software. It seems like all of the big guys use it- Twitter, Facebook, and 37Signals to just name a few. Memcached really pioneered the idea of a separate, distributed cache layer for your application and was really the only option a few years ago. Memcached is similar to APC in a lot of ways, except that it runs as a threaded C server, so you need to go over the network to communicate with it. Compared to APC, communicating over the network is obviously slower but still ends up being extremely performant to the tune of 500,000+ operations per second. The major advantage is that with some simple hashing, multiple Memcached servers can effectively be grouped together to distribute your cached data and provide you with the resources of all of the servers combined. That is to say, if you had 10 Memcached servers, each with 4GB of memory, you’d effectively have a 40GB in-memory cache available to you.

Memcached provides all of the same features as APC, including CAS (check-and-set) tokens which allow you to avoid race-conditions by only allowing new values to be the written if they key hasn’t been modified.

That being said, I’m not going to cover Memcached in this chapter because Redis, which was released a couple of years ago, has far outpaced Memcached in terms of features and reliability while offering similar performance (aka, damn fast).

Stuck on Memcached?

If you like Memcached, or you’re already using it and stuck with it, I’ve included a short bonus chapter on Memcached. But you should seriously consider switching to Redis. It’s a drop in replacement.

Redis

Redis is where it’s at. If you’ve never used it before, you’re in for a treat- it’s got enough coolness to make your inner geek excited. Just like Memcached and APC, it can do key/value caching with GETandSETprimitives, but it also adds other data types like lists, hashes, and sorted sets. There are literally hundreds of commands, and I won’t be able to cover them all here, socheck out the Redis documentation⁷²to play with the commands. There’s even an in-line console that let’s you play with each command right on the website.

Remember, the entire dataset stored in Redis MUST fit into memory. While Redis does persist your data to disk, it only reads from disk during startup so you must always have more memory than data.

In document CONDICIONES GENERALES (página 65-87)