5 * Contains \Drupal\memcache\DrupalMemcacheBase.
8 namespace Drupal\memcache;
13 * Class DrupalMemcacheBase.
15 abstract class DrupalMemcacheBase implements DrupalMemcacheInterface {
18 * The memcache config object.
20 * @var \Drupal\memcache\DrupalMemcacheConfig
25 * The memcache object.
28 * E.g. \Memcache|\Memcached
33 * The hash algorithm to pass to hash(). Defaults to 'sha1'
37 protected $hashAlgorithm;
40 * The prefix memcache key for all keys.
47 * Constructs a DrupalMemcacheBase object.
49 * @param \Drupal\memcache\DrupalMemcacheConfig
50 * The memcache config object.
52 public function __construct(DrupalMemcacheConfig $settings) {
53 $this->settings = $settings;
55 $this->hashAlgorithm = $this->settings->get('key_hash_algorithm', 'sha1');
56 $this->prefix = $this->settings->get('key_prefix', '');
62 public function get($key) {
63 $full_key = $this->key($key);
64 return $this->memcache->get($full_key);
70 public function key($key) {
71 $full_key = urlencode($this->prefix . '-' . $key);
73 // Memcache only supports key lengths up to 250 bytes. If we have generated
74 // a longer key, we shrink it to an acceptable length with a configurable
75 // hashing algorithm. Sha1 was selected as the default as it performs
76 // quickly with minimal collisions.
77 if (strlen($full_key) > 250) {
78 $full_key = urlencode(hash($this->hashAlgorithm, $this->prefix . '-' . $key));
87 public function delete($key) {
88 $full_key = $this->key($key);
89 return $this->memcache->delete($full_key, 0);
95 public function flush() {
96 $this->memcache->flush();