/**
 * Quick memcache interaction helper has transaction support using \Sky\Memcache
 * Sample Usage:
 *      mem('myKey', $myValue); // srite my key/value pair to memcache
 *      echo mem('myKey');      // read the value stored in 'myKey' and echo it
 *      mem('myKey', null);     // delete myKey from memcache
 *
 * @param   mixed   $key        can be a string or an array of strings
 * @param   mixed   $value      if not set, will be a read, if null, delete, else set
 * @param   string  $duration   how long you want to store the value, ex: '10 minutes'
 * @return  mixed
 */
function mem($key, $value = '§k¥', $duration = null)
{
    if ($value == '§k¥') {
        return \Sky\Memcache::get($key);
    } else {
        if (!is_null($value)) {
            return \Sky\Memcache::set($key, $value, $duration);
        } else {
            return \Sky\Memcache::delete($key);
        }
    }
}
Exemplo n.º 2
0
 /**
  * Deletes the current record if there is a token and identifier
  * by setting active = 0
  * and clears memcache
  *
  * Triggers events: before_delete and after_delete
  * Refreshes $this->_belongs_to
  *
  * @return array   the response array (after_fail or after_save)
  */
 public function delete()
 {
     if ($this->_in_transaction) {
         $this->addInternalError('fatal_error', array('message' => 'Cannot delete current instance while in transaction.', 'type' => 'invalid_action'));
         return;
     }
     $id = $this->getID();
     if ($this->_use_token_validation && ($this->_token != Model::generateToken($id, $this->_primary_table) || !$this->_token)) {
         $this->addInternalError('invalid_token');
     }
     if (!$id) {
         $this->addInternalError('identifier_not_set_for_delete');
     }
     if ($this->_errors) {
         return $this->errorResponse();
     }
     $this->_in_transaction = true;
     $this->startTransaction();
     $now = aql::now();
     $fields = array('active' => 0, 'mod_time' => $now, 'update_time' => $now);
     if (defined('PERSON_ID') && PERSON_ID) {
         $fields['mod__person_id'] = $fields['update__person_id'] = PERSON_ID;
     }
     # load the object
     # so that we have the information left over for after_delete hooks
     $this->loadDB($id);
     if ($this->methodExists('beforeDelete')) {
         $this->tryCallable(array($this, 'beforeDelete'));
     }
     if ($this->hasFailedTransaction() || $this->_errors) {
         return $this->getTransactionResponse(true);
     }
     if (aql::update($this->_primary_table, $fields, $id)) {
         # clears the memcache of stored objects of this identifier.
         $delete_key = function ($m) use($id) {
             $tmp = new $m();
             $key = $tmp->getMemKey($id);
             \Sky\Memcache::delete($key);
         };
         $delete_key($this->_model_name);
         foreach ($this->getModelDependencies() as $m) {
             $delete_key($m);
         }
         if ($this->methodExists('afterDelete')) {
             $this->tryCallable(array($this, 'afterDelete'));
         }
         if ($this->hasFailedTransaction() || $this->_errors) {
             return $this->getTransactionResponse(true);
         }
         $this->refreshBelongsTo();
         $re = $this->getTransactionResponse();
         unset($re['data'], $re['_token']);
         return $re;
     } else {
         $this->addInternalError('model_save_failure', array('message' => 'Error deleting record.'));
         return $this->getTransactionResponse(true);
     }
 }