Skip to content

Commit fdf568f

Browse files
author
Thomas Rabaix
committed
first commit (port of the SonataPageBundle)
0 parents  commit fdf568f

40 files changed

+2363
-0
lines changed

Adapter/ApcCache.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
/*
3+
* This file is part of the Sonata package.
4+
*
5+
* (c) Thomas Rabaix <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Sonata\CacheBundle\Adapter;
12+
13+
use Symfony\Component\Routing\Router;
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
16+
use Symfony\Component\Routing\RouterInterface;
17+
18+
use Sonata\BlockBundle\Cache\CacheInterface;
19+
use Sonata\CacheBundle\Cache\CacheElement;
20+
21+
class ApcCache implements CacheInterface
22+
{
23+
protected $servers;
24+
25+
protected $prefix;
26+
27+
protected $collection;
28+
29+
protected $router;
30+
31+
/**
32+
* @param \Symfony\Component\Routing\RouterInterface $router
33+
* @param string $token
34+
* @param string $prefix
35+
* @param array $servers
36+
*/
37+
public function __construct(RouterInterface $router, $token, $prefix, array $servers)
38+
{
39+
$this->token = $token;
40+
$this->prefix = $prefix;
41+
$this->servers = $servers;
42+
$this->router = $router;
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
private function getToken()
49+
{
50+
return $this->token;
51+
}
52+
53+
/**
54+
* @return bool
55+
*/
56+
public function flushAll()
57+
{
58+
$result = true;
59+
foreach ($this->servers as $server) {
60+
if (count(explode('.', $server['ip']) == 3)) {
61+
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
62+
} else {
63+
$socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP);
64+
}
65+
66+
// generate the raw http request
67+
$command = sprintf("GET %s HTTP/1.1\r\n", $this->router->generate('sonata_cache_apc', array('token' => $this->token)));
68+
$command .= sprintf("Host: %s\r\n", $server['domain']);
69+
$command .= "Connection: Close\r\n\r\n";
70+
71+
// setup the default timeout (avoid max execution time)
72+
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 2, 'usec' => 0));
73+
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 2, 'usec' => 0));
74+
75+
socket_connect($socket, $server['ip'], $server['port']);
76+
77+
socket_write($socket, $command);
78+
79+
$content = socket_read($socket, 1024);
80+
81+
if ($result) {
82+
$result = substr($content, -2) == 'ok' ? true : false;
83+
}
84+
}
85+
86+
return $result;
87+
}
88+
89+
/**
90+
* @param array $keys
91+
* @return bool
92+
*/
93+
public function flush(array $keys = array())
94+
{
95+
return $this->flushAll();
96+
}
97+
98+
/**
99+
* @param CacheElement $cacheElement
100+
* @return bool|\string[]
101+
*/
102+
public function has(CacheElement $cacheElement)
103+
{
104+
return apc_exists($this->computeCacheKeys($cacheElement));
105+
}
106+
107+
/**
108+
* @param CacheElement $cacheElement
109+
* @return mixed
110+
*/
111+
public function set(CacheElement $cacheElement)
112+
{
113+
$return = apc_store(
114+
$this->computeCacheKeys($cacheElement),
115+
$cacheElement->getValue(),
116+
$cacheElement->getTtl()
117+
);
118+
119+
return $return;
120+
}
121+
122+
/**
123+
* @param CacheElement $cacheElement
124+
* @return string
125+
*/
126+
private function computeCacheKeys(CacheElement $cacheElement)
127+
{
128+
$keys = $cacheElement->getKeys();
129+
130+
ksort($keys);
131+
132+
return md5($this->prefix.serialize($keys));
133+
}
134+
135+
/**
136+
* @param CacheElement $cacheElement
137+
* @return mixed
138+
*/
139+
public function get(CacheElement $cacheElement)
140+
{
141+
return apc_fetch($this->computeCacheKeys($cacheElement));
142+
}
143+
144+
/**
145+
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
146+
* @param $token
147+
* @return \Symfony\Component\HttpFoundation\Response
148+
*/
149+
public function cacheAction($token)
150+
{
151+
if ($this->getToken() == $token) {
152+
apc_clear_cache('user');
153+
154+
return new Response('ok', 200, array(
155+
'Cache-Control' => 'no-cache, must-revalidate'
156+
));
157+
}
158+
159+
throw new AccessDeniedException('invalid token');
160+
}
161+
162+
/**
163+
* @return bool
164+
*/
165+
public function isContextual()
166+
{
167+
return false;
168+
}
169+
}

Adapter/MemcachedCache.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/*
3+
* This file is part of the Sonata package.
4+
*
5+
* (c) Thomas Rabaix <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Sonata\CacheBundle\Adapter;
12+
13+
use Symfony\Component\Routing\Router;
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
use Sonata\BlockBundle\Cache\CacheInterface;
17+
use Sonata\CacheBundle\Cache\CacheElement;
18+
19+
class MemcachedCache implements CacheInterface
20+
{
21+
protected $servers;
22+
23+
protected $prefix;
24+
25+
protected $collection;
26+
27+
/**
28+
* @param $prefix
29+
* @param array $servers
30+
*/
31+
public function __construct($prefix, array $servers)
32+
{
33+
$this->prefix = $prefix;
34+
$this->servers = $servers;
35+
}
36+
37+
/**
38+
* @return bool
39+
*/
40+
public function flushAll()
41+
{
42+
return $this->getCollection()->flush();
43+
}
44+
45+
/**
46+
* @param array $keys
47+
* @return bool
48+
*/
49+
public function flush(array $keys = array())
50+
{
51+
return $this->getCollection()->delete($this->computeCacheKeys(new CacheElement($keys)));
52+
}
53+
54+
/**
55+
* @param CacheElement $cacheElement
56+
* @return bool
57+
*/
58+
public function has(CacheElement $cacheElement)
59+
{
60+
return $this->getCollection()->get($this->computeCacheKeys($cacheElement)) !== false;
61+
}
62+
63+
/**
64+
* @return \Memcached
65+
*/
66+
private function getCollection()
67+
{
68+
if (!$this->collection) {
69+
$this->collection = new \Memcached();
70+
71+
foreach ($this->servers as $server) {
72+
$this->collection->addServer($server['host'], $server['port'], $server['weight']);
73+
}
74+
}
75+
76+
return $this->collection;
77+
}
78+
79+
/**
80+
* @param CacheElement $cacheElement
81+
* @return mixed
82+
*/
83+
public function set(CacheElement $cacheElement)
84+
{
85+
$return = $this->getCollection()->set(
86+
$this->computeCacheKeys($cacheElement),
87+
$cacheElement->getValue(),
88+
time() + $cacheElement->getTtl()
89+
);
90+
91+
return $return;
92+
}
93+
94+
/**
95+
* @param CacheElement $cacheElement
96+
* @return string
97+
*/
98+
private function computeCacheKeys(CacheElement $cacheElement)
99+
{
100+
$keys = $cacheElement->getKeys();
101+
102+
ksort($keys);
103+
104+
return md5($this->prefix.serialize($keys));
105+
}
106+
107+
/**
108+
* @param CacheElement $cacheElement
109+
* @return mixed
110+
*/
111+
public function get(CacheElement $cacheElement)
112+
{
113+
return $this->getCollection()->get($this->computeCacheKeys($cacheElement));
114+
}
115+
116+
/**
117+
* @return bool
118+
*/
119+
public function isContextual()
120+
{
121+
return false;
122+
}
123+
}

0 commit comments

Comments
 (0)