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);
65 $track_errors = ini_set('track_errors', '1');
67 $result = @$this->memcache->get($full_key);
69 if (!empty($php_errormsg)) {
70 register_shutdown_function('memcache_log_warning', LogLevel::WARNING, 'Exception caught in DrupalMemcacheBase::get: !msg', array('!msg' => $php_errormsg));
73 ini_set('track_errors', $track_errors);
81 public function key($key) {
82 $full_key = urlencode($this->prefix . '-' . $key);
84 // Memcache only supports key lengths up to 250 bytes. If we have generated
85 // a longer key, we shrink it to an acceptable length with a configurable
86 // hashing algorithm. Sha1 was selected as the default as it performs
87 // quickly with minimal collisions.
88 if (strlen($full_key) > 250) {
89 $full_key = urlencode(hash($this->hashAlgorithm, $this->prefix . '-' . $key));
98 public function delete($key) {
99 $full_key = $this->key($key);
100 return $this->memcache->delete($full_key, 0);
106 public function flush() {
107 $this->memcache->flush();