/**
  * Calls the RECache algorithm to facilitate the magic-caching of a
  * controller function's output.
  *
  * @param key string The key name to be cached.
  * @param ttl int The number of seconds until this key should expire.
  * @param groups array|string|boolean The group name, or array of group
  * 		names, to associate the key with; or false to not associate the
  * 		key with any group.
  * @param callback callback A properly formatted callback to a function
  * 		within this (extended) class.  The result of calling this function
  * 		will be cached.
  * @param args array|boolean An array of arguments to send to the callback
  * 		function, or false for no arguments.
  * @return mixed The return value of the callback function.
  */
 protected function __recache(&$key, &$ttl, &$groups, $callback, &$args)
 {
     $cm = RECacheManager::getInstance();
     $data = $cm->recache_get($key, $ttl, $groups, array($this, "__getOutput"), array($callback, $args));
     if (count($data) === 2) {
         echo $data[1];
         return $data[0];
     }
     // In very rare cases, cached data might be wrong.
     Log::warn("Cached data for controller " . get_class($this) . " was not properly formatted.  Calling " . (isset($callback[1]) ? $callback[1] : "function") . " directly to recover.");
     return call_user_func_array($callback, $args);
 }
Beispiel #2
0
use appdb\usersession\UserSession;
use hydrogen\recache\RECacheManager;
// Open up our session
UserSession::open_session(false);
if (!($ubean = UserSession::getUserBean())) {
    die('Hey. Log in.');
}
// Are we allowed in?
$group = $ubean->getMapped('group');
if ($group->group_name != 'Administrator' && $group->group_name != 'Moderator') {
    die('Your permissions suck.');
}
$reset = false;
$success = false;
if (isset($_POST['submit'])) {
    $cm = RECacheManager::getInstance();
    if (isset($_POST['apiprofiles'])) {
        $success = $cm->clearGroup('apiprofiles');
        $reset = "apiprofiles";
    } else {
        if (isset($_POST['appdetails'])) {
            $success = $cm->clearGroup('appdetails');
            $reset = "appdetails";
        } else {
            if (isset($_POST['applinks'])) {
                $success = $cm->clearGroup('applinks');
                $reset = "applinks";
            } else {
                if (isset($_POST['applist'])) {
                    $success = $cm->clearGroup('applist');
                    $reset = "applist";
Beispiel #3
0
 /**
  * Runs the router, iteratively looking for a match to each rule in the
  * order in which the rules were set up.  When a rule matches, its
  * respective controller function is called and the router exits, ignoring
  * the remaining rules.
  *
  * @return boolean true if a rule has matched and the controller/function
  * 		was successfully called; false otherwise.
  */
 public function start()
 {
     // Get a proper PATH_INFO
     $path = isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] ? $_SERVER['PATH_INFO'] : null;
     if (!$path) {
         $path = isset($_GET[self::PATH_GET_VAR]) && $_GET[self::PATH_GET_VAR] ? $_GET[self::PATH_GET_VAR] : '/';
     }
     $cm = RECacheManager::getInstance();
     // Cache the rules if they're not already there.
     if ($this->doRuleCache && !$this->rulesFromCache) {
         $cm->set("Router:" . $this->name, $this->ruleSet, $this->expireTime !== null ? $this->expireTime : self::DEFAULT_CACHE_TIME, $this->groups);
     }
     // Is this exact route cached?
     if ($this->doRouteCache && ($rule = $cm->get("Router:" . $this->name . ":{$path}"))) {
         $success = $this->passRequest($rule['controller'], $rule['function'], $rule['args'], $rule['argProtect']);
         if ($success) {
             return true;
         }
     }
     // Iterate through the rules until a match is found
     foreach ($this->ruleSet as $rule) {
         if ((!$rule['method'] || $rule['method'] == $_SERVER['REQUEST_METHOD']) && preg_match($rule['regex'], $path, $vars)) {
             // Collect all the variables
             if ($rule['defaults']) {
                 $vars = array_merge($rule['defaults'], $vars);
             }
             // Apply the overrides
             $arraysAsParams = array();
             if ($rule['overrides']) {
                 $this->applyOverrides($vars, $rule['overrides'], $arraysAsParams);
             }
             // At this point, we must have a controller and function
             if (!isset($vars[self::KEYWORD_CONTROLLER]) || !isset($vars[self::KEYWORD_FUNCTION])) {
                 throw new RouteSyntaxException("Matched route requires both a '" . self::KEYWORD_CONTROLLER . "' and a '" . self::KEYWORD_FUNCTION . "' variable.");
             }
             // Collect the arguments to be sent to the function in the
             // requested format.
             $args = array();
             $variableParams = false;
             if ($rule['args']) {
                 // Array keys format
                 if (isset($rule['argArray']) && $rule['argArray']) {
                     foreach ($rule['args'] as $key) {
                         if (isset($vars[$key])) {
                             $args[$key] = $vars[$key];
                         }
                     }
                     $args = array($args);
                 } else {
                     $variableParams = true;
                     foreach ($rule['args'] as $key) {
                         if (isset($vars[$key])) {
                             if (is_array($vars[$key]) && isset($arraysAsParams[$key])) {
                                 $args = array_merge($args, $vars[$key]);
                             } else {
                                 $args[] = $vars[$key];
                             }
                         }
                     }
                 }
             }
             // Pass the request!
             $success = $this->passRequest($vars[self::KEYWORD_CONTROLLER], $vars[self::KEYWORD_FUNCTION], $args, $variableParams);
             if ($success) {
                 // Cache the route
                 if ($this->doRouteCache) {
                     $cm->set("Router:" . $this->name . ":{$path}", array('controller' => $vars[self::KEYWORD_CONTROLLER], 'function' => $vars[self::KEYWORD_FUNCTION], 'args' => $args, 'argProtect' => $variableParams), $this->expireTime ?: self::DEFAULT_CACHE_TIME, $this->groups);
                 }
                 return true;
             }
         }
     }
     return false;
 }
 /**
  * Calls the RECache algorithm to facilitate the magic-caching of a
  * certain callback function.  By default, it caches the 'return' value of
  * the callback function, and returns that data if it's found in the cache.
  * This function can be overridden to cache, return, or output different
  * data.
  *
  * @param key string The key name to be cached.
  * @param ttl int The number of seconds until this key should expire.
  * @param groups array|string|boolean The group name, or array of group
  * 		names, to associate the key with; or false to not associate the
  * 		key with any group.
  * @param callback callback A properly formatted callback to a function
  * 		within this (extended) class.  The result of calling this function
  * 		will be cached.
  * @param args array|boolean An array of arguments to send to the callback
  * 		function, or false for no arguments.
  * @return mixed The cached value or the result of the callback function,
  * 		depending on whether a value was found in the cache.
  */
 protected function __recache(&$key, &$ttl, &$groups, $callback, &$args)
 {
     $cm = RECacheManager::getInstance();
     return $cm->recache_get($key, $ttl, $groups, $callback, $args);
 }
Beispiel #5
0
 function __construct()
 {
     $this->cm = RECacheManager::getInstance();
 }