/** * Logical implementation of the store() command */ public function store(Memento\Key $key, $value, $expires) { // memcache can't store expires longer than 30 days if ($expires > self::MAX_EXPIRES) { return false; } // get primary key string $keyStr = $this->groupKey ? $this->groupKey->getKey() : $key->getKey(); if (!$this->__connect($key)) { return false; } // primary key stores a basic map, potentially to other keys $meta = array(Memento\Hash::FIELD_CREATED => time(), Memento\Hash::FIELD_EXPIRES => $expires, Memento\Hash::FIELD_VALID => $keyStr . '::' . $keyStr . Memento\Hash::FIELD_VALID); // handle arguments based on group or single key if ($this->groupKey) { // group keys store a keys field which points to a key storing the key map $meta[Memento\Hash::FIELD_KEYS] = $keyStr . '::' . $keyStr . Memento\Hash::FIELD_KEYS; // key which will contain the data $singleKey = $meta[Memento\Hash::FIELD_KEYS] . '_' . $key->getKey(); // initial key map $keys = array($key->getKey() => $singleKey); // valid state $valid = true; // look for existing key map $keys = $this->memcache->get($meta[Memento\Hash::FIELD_KEYS]); if (!is_array($keys)) { $keys = array(); } $keys[$key->getKey()] = $singleKey; // update the keys map $this->memcache->set($meta[Memento\Hash::FIELD_KEYS], $keys, null, $expires); // save the data with mapped key $this->memcache->set($singleKey, $value, null, $expires); } else { // store data in meta $meta[Memento\Hash::FIELD_DATA] = $keyStr . '::' . $keyStr . Memento\Hash::FIELD_DATA; // valid state $valid = true; // store data $this->memcache->set($meta[Memento\Hash::FIELD_DATA], $value, null, $expires); } // store meta data $metaStored = $this->memcache->set($keyStr, $meta, null, $expires); // store valid state $validStored = $this->memcache->set($meta[Memento\Hash::FIELD_VALID], $valid, null, $expires); $this->__disconnect(); if ($metaStored && $validStored) { return true; } return false; }
/** * Logical implementation of the retrieve() command */ public function retrieve(Memento\Key $key) { if ($this->groupKey) { $data = $this->redis->hget($this->groupKey->getKey(), $key->getKey()); } else { $data = $this->redis->hget($key->getKey(), Memento\Hash::FIELD_DATA); } // unserialize data $value = unserialize($data); if (false === $value) { return NULL; } return $value; }