source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/Common/Cache/MemcachedCache.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 3.3 KB
Line 
1<?php
2
3/*
4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 *
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the LGPL. For more information, see
18 * <http://www.doctrine-project.org>.
19 */
20
21namespace Doctrine\Common\Cache;
22
23use \Memcached;
24
25/**
26 * Memcached cache provider.
27 *
28 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29 * @link    www.doctrine-project.org
30 * @since   2.2
31 * @author  Benjamin Eberlei <kontakt@beberlei.de>
32 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
33 * @author  Jonathan Wage <jonwage@gmail.com>
34 * @author  Roman Borschel <roman@code-factory.org>
35 * @author  David Abdemoulaie <dave@hobodave.com>
36 */
37class MemcachedCache extends CacheProvider
38{
39    /**
40     * @var Memcached
41     */
42    private $memcached;
43
44    /**
45     * Sets the memcache instance to use.
46     *
47     * @param Memcached $memcached
48     */
49    public function setMemcached(Memcached $memcached)
50    {
51        $this->memcached = $memcached;
52    }
53
54    /**
55     * Gets the memcached instance used by the cache.
56     *
57     * @return Memcached
58     */
59    public function getMemcached()
60    {
61        return $this->memcached;
62    }
63
64    /**
65     * {@inheritdoc}
66     */
67    protected function doFetch($id)
68    {
69        return $this->memcached->get($id);
70    }
71
72    /**
73     * {@inheritdoc}
74     */
75    protected function doContains($id)
76    {
77        return (false !== $this->memcached->get($id));
78    }
79
80    /**
81     * {@inheritdoc}
82     */
83    protected function doSave($id, $data, $lifeTime = 0)
84    {
85        if ($lifeTime > 30 * 24 * 3600) {
86            $lifeTime = time() + $lifeTime;
87        }
88        return $this->memcached->set($id, $data, (int) $lifeTime);
89    }
90
91    /**
92     * {@inheritdoc}
93     */
94    protected function doDelete($id)
95    {
96        return $this->memcached->delete($id);
97    }
98
99    /**
100     * {@inheritdoc}
101     */
102    protected function doFlush()
103    {
104        return $this->memcached->flush();
105    }
106
107    /**
108     * {@inheritdoc}
109     */
110    protected function doGetStats()
111    {
112        $stats   = $this->memcached->getStats();
113        $servers = $this->memcached->getServerList();
114        $key     = $servers[0]['host'] . ':' . $servers[0]['port'];
115        $stats   = $stats[$key];
116        return array(
117            Cache::STATS_HITS   => $stats['get_hits'],
118            Cache::STATS_MISSES => $stats['get_misses'],
119            Cache::STATS_UPTIME => $stats['uptime'],
120            Cache::STATS_MEMORY_USAGE       => $stats['bytes'],
121            Cache::STATS_MEMORY_AVAILIABLE  => $stats['limit_maxbytes'],
122        );
123    }
124}
Note: See TracBrowser for help on using the repository browser.