1: <?php namespace Laravel;
2:
3: class Memcached {
4:
5: /**
6: * The Memcached connection instance.
7: *
8: * @var Memcached
9: */
10: protected static $connection;
11:
12: /**
13: * Get the Memcached connection instance.
14: *
15: * <code>
16: * // Get the Memcache connection and get an item from the cache
17: * $name = Memcached::connection()->get('name');
18: *
19: * // Get the Memcache connection and place an item in the cache
20: * Memcached::connection()->set('name', 'Taylor');
21: * </code>
22: *
23: * @return Memcached
24: */
25: public static function connection()
26: {
27: if (is_null(static::$connection))
28: {
29: static::$connection = static::connect(Config::get('cache.memcached'));
30: }
31:
32: return static::$connection;
33: }
34:
35: /**
36: * Create a new Memcached connection instance.
37: *
38: * @param array $servers
39: * @return Memcached
40: */
41: protected static function connect($servers)
42: {
43: $memcache = new \Memcached;
44:
45: foreach ($servers as $server)
46: {
47: $memcache->addServer($server['host'], $server['port'], $server['weight']);
48: }
49:
50: if ($memcache->getVersion() === false)
51: {
52: throw new \Exception('Could not establish memcached connection.');
53: }
54:
55: return $memcache;
56: }
57:
58: /**
59: * Dynamically pass all other method calls to the Memcache instance.
60: *
61: * <code>
62: * // Get an item from the Memcache instance
63: * $name = Memcached::get('name');
64: *
65: * // Store data on the Memcache server
66: * Memcached::set('name', 'Taylor');
67: * </code>
68: */
69: public static function __callStatic($method, $parameters)
70: {
71: return call_user_func_array(array(static::connection(), $method), $parameters);
72: }
73:
74: }