get(self::getRealKey($key, $group_name)); } else { return null; } } /** * Set the value to a key. * * This method returns true on success and false on failure. * $ttl is measured in seconds. If it is zero, the key should not expire. * * @param string $key * @param mixed $value * @param int $ttl (optional) * @param string $group_name (optional) * @return bool */ public static function set($key, $value, $ttl = 0, $group_name = null) { if (self::$_driver !== null) { if ($ttl >= (3600 * 24 * 30)) { $ttl = min(3600 * 24 * 30, max(0, $ttl - time())); } return self::$_driver->set(self::getRealKey($key, $group_name), $value, intval($ttl)) ? true : false; } else { return false; } } /** * Delete a key. * * This method returns true on success and false on failure. * If the key does not exist, it should return false. * * @param string $key * @param string $group_name (optional) * @return bool */ public static function delete($key, $group_name = null) { if (self::$_driver !== null) { return self::$_driver->delete(self::getRealKey($key, $group_name)) ? true : false; } else { return false; } } /** * Check if a key exists. * * This method returns true on success and false on failure. * * @param string $key * @param string $group_name (optional) * @return bool */ public static function exists($key, $group_name = null) { if (self::$_driver !== null) { return self::$_driver->exists(self::getRealKey($key, $group_name)) ? true : false; } else { return false; } } /** * Clear a group of keys from the cache. * * This method returns true on success and false on failure. * * @param string $group_name * @return bool */ public static function clearGroup($group_name) { if (self::$_driver !== null) { return self::$_driver->incr(self::getRealKey('#GROUP:' . $group_name . ':v'), 1) ? true : false; } else { return false; } } /** * Clear all keys from the cache. * * This method returns true on success and false on failure. * * @return bool */ public static function clearAll() { if (self::$_driver !== null) { return self::$_driver->clear() ? true : false; } else { return false; } } /** * Get the actual key used by Rhymix. * * @param string $key * @param string $group_name (optional) * @param bool $add_prefix (optional) * @return string */ public static function getRealKey($key, $group_name = null, $add_prefix = true) { if ($group_name) { $group_version = intval(self::get('#GROUP:' . $group_name . ':v')); $key = '#GROUP:' . $group_name . ':' . $group_version . ':' . $key; } return ($add_prefix ? self::getCachePrefix() : '') . $key; } }