Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions src/object-cache.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,40 @@ public function get($key)

return $res;
}

/**
* Get cache, multiple keys at once
*
* @since 6.3
* @access public
*/
public function get_multiple($keys)
{
if (count($keys)<1) {
return array();
}

if (!$this->_cfg_enabled) {
return array();
}

if (!$this->_can_cache()) {
return array();
}

if (!$this->_connect()) {
return array();
}

if ($this->_oc_driver == 'Redis') {
$res = $this->_conn->mGet($keys);
}
else{
$res = $this->_conn->getMulti($keys, \Memcached::GET_PRESERVE_ORDER);
}

return $res;
}

/**
* Set cache
Expand Down Expand Up @@ -502,6 +536,78 @@ public function set($key, $data, $expire)
return $res;
}

/**
* Test if Redis has Pipeline enabled
*
* @since 6.3
* @access private
*/
private function redis_has_pipeline(){
return method_exists( $this->_conn, 'pipeline' );
}

/**
* Set multiple cache
*
* @since 6.3
* @access public
*/
public function set_multiple($data, $group, $expire)
{
$res = array();

if (!$this->_cfg_enabled) {
return null;
}

if (!$this->_connect()) {
return null;
}

$ttl = $expire ?: $this->_cfg_life;
$keys = array_keys($data);

if (
$this->_oc_driver == 'Redis' &&
$this->redis_has_pipeline() &&
! $this->is_non_persistent($group)
) {
try {
// Create pipeline.
$pipeline = $this->_conn->pipeline();
// Add action to pipeline.
foreach($data as $key=>$value){
if ( $expire ) {
$pipeline->setex( $key, $ttl, $value );
} else {
$pipeline->set( $key, $value );
}
}
// Run the pipeline and get values.
$exec = $pipeline->exec();

// Prepare return
foreach ( $exec as $i => $result) {
$res[$keys[$i]] = $result;
}

return $res;
} catch (\Exception $ex) {
return false;
}
}
elseif ($this->_oc_driver == 'Memcached') {
return $this->_conn->setMulti($data, $ttl);
}

// Fallback: Do normal set in case of error.
foreach($data as $key => $value){
$res[$key] = $this->set($key, $value, $ttl);
}

return $res;
}

/**
* Check if can cache or not
*
Expand Down Expand Up @@ -541,6 +647,51 @@ public function delete($key)
return (bool) $res;
}

/**
* Delete multiple cache at once
*
* @since 6.3
* @access public
*/
public function delete_multiple($keys, $group)
{
if (!$this->_cfg_enabled) {
return null;
}

if (!$this->_connect()) {
return null;
}

if (
$this->_oc_driver == 'Redis' &&
$this->redis_has_pipeline() &&
! $this->is_non_persistent($group)
) {
try {
// Create pipeline.
$pipeline = $this->_conn->pipeline();
// Add action to pipeline.
foreach($keys as $key){
$pipeline->del($key);
}

// Run the pipeline and get status.
$exec = $pipeline->exec();
foreach ($exec as $i => $result) {
$res[$keys[$i]] = $result;
}

return $res;
} catch (\Exception $ex) {
return false;
}
}
elseif ($this->_oc_driver == 'Memcached') {
return $this->_conn->deleteMulti($keys);
}
}

/**
* Clear all cache
*
Expand Down Expand Up @@ -570,6 +721,17 @@ public function flush()
return $res;
}

/**
* Closes the cache.
*
* @since 6.3
* @access public
*/
public function close()
{
return true;
}

/**
* Add global groups
*
Expand Down
Loading