* @package generis */ class core_kernel_users_Cache { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * Short description of attribute SERIAL_PREFIX_INCLUDED_ROLES * * @access public * @var string */ const SERIAL_PREFIX_INCLUDED_ROLES = 'roles-ir'; // --- OPERATIONS --- /** * Retrieve roles included in a given Generis Role from the Cache memory. * * @access public * @author Jerome Bogaerts, * * @param core_kernel_classes_Resource $role A Generis Role Resource. * * @return array * @throws \oat\oatbox\service\ServiceNotFoundException * @throws common_Exception * @throws core_kernel_users_CacheException */ public static function retrieveIncludedRoles(core_kernel_classes_Resource $role) { $returnValue = []; $serial = self::buildIncludedRolesSerial($role); $fromCache = ServiceManager::getServiceManager()->get(SimpleCache::SERVICE_ID)->get($serial); // array of URIs. if (is_null($fromCache)) { $roleUri = $role->getUri(); $msg = "Includes roles related to Role with URI '${roleUri}' is not in the Cache memory."; throw new core_kernel_users_CacheException($msg); } foreach ($fromCache as $uri) { $returnValue[$uri] = new core_kernel_classes_Resource($uri); } return (array) $returnValue; } /** * Put roles included in a Generis Role in the Cache memory. * * @access public * @author Jerome Bogaerts, * * @param core_kernel_classes_Resource $role * @param array $includedRoles * * @return bool * @throws \oat\oatbox\service\ServiceNotFoundException * @throws common_Exception * @throws core_kernel_users_CacheException */ public static function cacheIncludedRoles(core_kernel_classes_Resource $role, $includedRoles) { // Make a simple array of URIs with the included roles. $toCache = []; foreach ($includedRoles as $resource) { $toCache[] = $resource->getUri(); } $serial = self::buildIncludedRolesSerial($role); /** @var SimpleCache $cache */ $cache = ServiceManager::getServiceManager()->get(SimpleCache::SERVICE_ID); try { $cache->set($serial, $toCache); $returnValue = true; } catch (common_Exception $e) { $roleUri = $role->getUri(); $msg = "An error occurred while writing included roles in the cache memory for Role '${roleUri}': "; $msg .= $e->getMessage(); throw new core_kernel_users_CacheException($msg); } return (bool) $returnValue; } /** * Remove roles included in a Generis Role from the Cache memory. * * @access public * @author Jerome Bogaerts, * @param core_kernel_classes_Resource $role A Generis Role as a Resource. * @return boolean */ public static function removeIncludedRoles(core_kernel_classes_Resource $role) { $serial = self::buildIncludedRolesSerial($role); ServiceManager::getServiceManager()->get('generis/cache')->remove($serial); ; // -- note: the cache might exist even if it was successfully // removed due to race conditions. // $returnValue = (file_exists(GENERIS_CACHE_PATH . $serial)) ? false : true; return true; } /** * Returns true if the roles included in a given Generis Role are in the * memory. * * @access public * @author Jerome Bogaerts, * @param core_kernel_classes_Resource $role The Generis Role you want to check if its included roles are in the cache memory. * @return boolean */ public static function areIncludedRolesInCache(core_kernel_classes_Resource $role) { $serial = self::buildIncludedRolesSerial($role); return ServiceManager::getServiceManager()->get('generis/cache')->has($serial); } /** * Build a serial aiming at identifying the includes roles of a given * Role in the Cache memory. * * @access private * @author Jerome Bogaerts, * @param core_kernel_classes_Resource $role The role you want to create a serial for. * @return string */ private static function buildIncludedRolesSerial(core_kernel_classes_Resource $role) { return self::SERIAL_PREFIX_INCLUDED_ROLES . urlencode($role->getUri()); } /** * Removes all entries related to included roles from the Cache memory. * * @access public * @author Jerome Bogaerts, * @return void */ public static function flush() { ServiceManager::getServiceManager()->get('generis/cache')->purge(); } }