Overview

Namespaces

  • Budabot
    • Core
      • Modules
    • User
      • Modules
  • None
  • Tyrence
    • Modules

Classes

  • AccessLevel
  • Budabot\Core\AccessManager
  • Budabot\Core\AdminManager
  • Budabot\Core\AOChat
  • Budabot\Core\AOChatPacket
  • Budabot\Core\AOChatQueue
  • Budabot\Core\AOExtMsg
  • Budabot\Core\AsyncHttp
  • Budabot\Core\AutoInject
  • Budabot\Core\BotRunner
  • Budabot\Core\Budabot
  • Budabot\Core\BuddylistManager
  • Budabot\Core\CacheManager
  • Budabot\Core\CacheResult
  • Budabot\Core\ClassLoader
  • Budabot\Core\ColorSettingHandler
  • Budabot\Core\CommandAlias
  • Budabot\Core\CommandManager
  • Budabot\Core\ConfigFile
  • Budabot\Core\DB
  • Budabot\Core\DBRow
  • Budabot\Core\EventLoop
  • Budabot\Core\EventManager
  • Budabot\Core\GuildChannelCommandReply
  • Budabot\Core\GuildManager
  • Budabot\Core\HelpManager
  • Budabot\Core\Http
  • Budabot\Core\HttpRequest
  • Budabot\Core\LegacyLogger
  • Budabot\Core\LimitsController
  • Budabot\Core\LoggerWrapper
  • Budabot\Core\MMDBParser
  • Budabot\Core\Modules\AdminController
  • Budabot\Core\Modules\AliasController
  • Budabot\Core\Modules\AltInfo
  • Budabot\Core\Modules\AltsController
  • Budabot\Core\Modules\BanController
  • Budabot\Core\Modules\BuddylistController
  • Budabot\Core\Modules\ColorsController
  • Budabot\Core\Modules\CommandlistController
  • Budabot\Core\Modules\CommandSearchController
  • Budabot\Core\Modules\ConfigController
  • Budabot\Core\Modules\EventlistController
  • Budabot\Core\Modules\HelpController
  • Budabot\Core\Modules\LogsController
  • Budabot\Core\Modules\PlayerLookupController
  • Budabot\Core\Modules\ProfileCommandReply
  • Budabot\Core\Modules\ProfileController
  • Budabot\Core\Modules\SettingsController
  • Budabot\Core\Modules\SQLController
  • Budabot\Core\Modules\SystemController
  • Budabot\Core\Modules\UsageController
  • Budabot\Core\Modules\WhitelistController
  • Budabot\Core\NumberSettingHandler
  • Budabot\Core\OptionsSettingHandler
  • Budabot\Core\PlayerHistory
  • Budabot\Core\PlayerHistoryManager
  • Budabot\Core\PlayerManager
  • Budabot\Core\Preferences
  • Budabot\Core\PrivateChannelCommandReply
  • Budabot\Core\PrivateMessageCommandReply
  • Budabot\Core\Registry
  • Budabot\Core\SettingHandler
  • Budabot\Core\SettingManager
  • Budabot\Core\SettingObject
  • Budabot\Core\SocketManager
  • Budabot\Core\SocketNotifier
  • Budabot\Core\SubcommandManager
  • Budabot\Core\Text
  • Budabot\Core\TextSettingHandler
  • Budabot\Core\Timer
  • Budabot\Core\TimerEvent
  • Budabot\Core\TimeSettingHandler
  • Budabot\Core\Util
  • Budabot\Core\xml
  • Budabot\User\Modules\AlienArmorController
  • Budabot\User\Modules\AlienBioController
  • Budabot\User\Modules\AlienMiscController
  • Budabot\User\Modules\AOSpeakController
  • Budabot\User\Modules\AOUController
  • Budabot\User\Modules\AXPController
  • Budabot\User\Modules\BankController
  • Budabot\User\Modules\BosslootController
  • Budabot\User\Modules\BroadcastController
  • Budabot\User\Modules\BuffPerksController
  • Budabot\User\Modules\CacheController
  • Budabot\User\Modules\ChatAssistController
  • Budabot\User\Modules\ChatCheckController
  • Budabot\User\Modules\ChatLeaderController
  • Budabot\User\Modules\ChatRallyController
  • Budabot\User\Modules\ChatSayController
  • Budabot\User\Modules\ChatTopicController
  • Budabot\User\Modules\CityWaveController
  • Budabot\User\Modules\CloakController
  • Budabot\User\Modules\ClusterController
  • Budabot\User\Modules\CountdownController
  • Budabot\User\Modules\DevController
  • Budabot\User\Modules\DingController
  • Budabot\User\Modules\EventsController
  • Budabot\User\Modules\FightController
  • Budabot\User\Modules\FindOrgController
  • Budabot\User\Modules\FindPlayerController
  • Budabot\User\Modules\FunController
  • Budabot\User\Modules\GitController
  • Budabot\User\Modules\GuideController
  • Budabot\User\Modules\GuildController
  • Budabot\User\Modules\HelpbotController
  • Budabot\User\Modules\HtmlDecodeController
  • Budabot\User\Modules\ImplantController
  • Budabot\User\Modules\ImplantDesignerController
  • Budabot\User\Modules\InactiveMemberController
  • Budabot\User\Modules\ItemsController
  • Budabot\User\Modules\KillOnSightController
  • Budabot\User\Modules\LevelController
  • Budabot\User\Modules\LinksController
  • Budabot\User\Modules\LootListsController
  • Budabot\User\Modules\MdbController
  • Budabot\User\Modules\MessageInfoCommandReply
  • Budabot\User\Modules\MockCommandReply
  • Budabot\User\Modules\NanoController
  • Budabot\User\Modules\NewsController
  • Budabot\User\Modules\NotesController
  • Budabot\User\Modules\OnlineController
  • Budabot\User\Modules\OrgHistoryController
  • Budabot\User\Modules\OrglistController
  • Budabot\User\Modules\OrgMembersController
  • Budabot\User\Modules\OSController
  • Budabot\User\Modules\PlayerHistoryController
  • Budabot\User\Modules\PlayfieldController
  • Budabot\User\Modules\PocketbossController
  • Budabot\User\Modules\PremadeImplantController
  • Budabot\User\Modules\PrivateChannelController
  • Budabot\User\Modules\QuoteController
  • Budabot\User\Modules\RaffleController
  • Budabot\User\Modules\RaidController
  • Budabot\User\Modules\RandomController
  • Budabot\User\Modules\RecipeController
  • Budabot\User\Modules\RelayController
  • Budabot\User\Modules\ReputationController
  • Budabot\User\Modules\ResearchController
  • Budabot\User\Modules\RunAsController
  • Budabot\User\Modules\SendTellController
  • Budabot\User\Modules\ShoppingController
  • Budabot\User\Modules\SilenceController
  • Budabot\User\Modules\SkillsController
  • Budabot\User\Modules\SpiritsController
  • Budabot\User\Modules\StopwatchController
  • Budabot\User\Modules\Teamspeak3
  • Budabot\User\Modules\TeamspeakController
  • Budabot\User\Modules\TestController
  • Budabot\User\Modules\TimeController
  • Budabot\User\Modules\TimerController
  • Budabot\User\Modules\TimezoneController
  • Budabot\User\Modules\TowerController
  • Budabot\User\Modules\TrackerController
  • Budabot\User\Modules\TrickleController
  • Budabot\User\Modules\UnixtimeController
  • Budabot\User\Modules\VoteController
  • Budabot\User\Modules\WeatherController
  • Budabot\User\Modules\WhatBuffsController
  • Budabot\User\Modules\WhereisController
  • Budabot\User\Modules\WhoisController
  • Budabot\User\Modules\WhoisOrgController
  • Budabot\User\Modules\WhompahController
  • Command
  • DefaultStatus
  • DefineCommand
  • Description
  • Event
  • HandlesCommand
  • Help
  • Inject
  • Instance
  • Intoptions
  • Matches
  • Options
  • Setting
  • Setup
  • Type
  • Tyrence\Modules\DemoResponseCommandReply
  • Tyrence\Modules\SameChannelResponseController
  • Visibility

Interfaces

  • Budabot\Core\CommandReply

Exceptions

  • Budabot\Core\InvalidHttpRequest
  • Budabot\Core\SQLException
  • Budabot\Core\StopExecutionException

Functions

  • Budabot\Core\isWindows
  • Budabot\Core\Modules\read_input
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Budabot\Core;
  4: 
  5: use Exception;
  6: 
  7: /**
  8:  * @Instance
  9:  */
 10: class CacheManager {
 11: 
 12:     /** @Inject */
 13:     public $chatBot;
 14: 
 15:     /** @Inject */
 16:     public $http;
 17: 
 18:     /** @Inject */
 19:     public $util;
 20: 
 21:     private $cacheDir;
 22: 
 23:     /** @Setup */
 24:     public function init() {
 25:         $this->cacheDir = $this->chatBot->vars["cachefolder"];
 26: 
 27:         //Making sure that the cache folder exists
 28:         if (!dir($this->cacheDir)) {
 29:             mkdir($this->cacheDir, 0777);
 30:         }
 31:     }
 32: 
 33:     public function lookup($url, $groupName, $filename, $isValidCallback, $maxCacheAge = 86400, $forceUpdate = false) {
 34:         if (empty($groupName)) {
 35:             throw new Exception("Cache group name cannot be empty");
 36:         }
 37: 
 38:         $cacheResult = new CacheResult();
 39: 
 40:         // Check if a xml file of the person exists and if it is uptodate
 41:         if (!$forceUpdate && $this->cacheExists($groupName, $filename)) {
 42:             $cacheAge = $this->getCacheAge($groupName, $filename);
 43:             if ($cacheAge < $maxCacheAge) {
 44:                 $data = $this->retrieve($groupName, $filename);
 45:                 if (call_user_func($isValidCallback, $data)) {
 46:                     $cacheResult->data = $data;
 47:                     $cacheResult->cacheAge = $cacheAge;
 48:                     $cacheResult->usedCache = true;
 49:                     $cacheResult->oldCache = false;
 50:                     $cacheResult->success = true;
 51:                 } else {
 52:                     unset($data);
 53:                     $this->remove($groupName, $filename);
 54:                 }
 55:             }
 56:         }
 57: 
 58:         //If no old history file was found or it was invalid try to update it from url
 59:         if ($cacheResult->success !== true) {
 60:             $response = $this->http->get($url)->waitAndReturnResponse();
 61:             $data = $response->body;
 62:             if (call_user_func($isValidCallback, $data)) {
 63:                 $cacheResult->data = $data;
 64:                 $cacheResult->cacheAge = 0;
 65:                 $cacheResult->usedCache = false;
 66:                 $cacheResult->oldCache = false;
 67:                 $cacheResult->success = true;
 68:             } else {
 69:                 unset($data);
 70:             }
 71:         }
 72: 
 73:         //If the site was not responding or the data was invalid and a xml file exists get that one
 74:         if ($cacheResult->success !== true && $this->cacheExists($groupName, $filename)) {
 75:             $data = $this->retrieve($groupName, $filename);
 76:             if (call_user_func($isValidCallback, $data)) {
 77:                 $cacheResult->data = $data;
 78:                 $cacheResult->cacheAge = $this->getCacheAge($groupName, $filename);
 79:                 $cacheResult->usedCache = true;
 80:                 $cacheResult->oldCache = true;
 81:                 $cacheResult->success = true;
 82:             } else {
 83:                 unset($data);
 84:                 $this->remove($groupName, $filename);
 85:             }
 86:         }
 87: 
 88:         // if a new file was downloaded, save it in the cache
 89:         if ($cacheResult->usedCache === false && $cacheResult->success === true) {
 90:             $this->store($groupName, $filename, $cacheResult->data);
 91:         }
 92: 
 93:         return $cacheResult;
 94:     }
 95: 
 96:     public function store($groupName, $filename, $contents) {
 97:         if (!dir($this->cacheDir . '/' . $groupName)) {
 98:             mkdir($this->cacheDir . '/' . $groupName, 0777);
 99:         }
100: 
101:         $cacheFile = "$this->cacheDir/$groupName/$filename";
102: 
103:         // at least in windows, modifcation timestamp will not change unless this is done
104:         // not sure why that is the case -tyrence
105:         @unlink($cacheFile);
106: 
107:         $fp = fopen($cacheFile, "w");
108:         fwrite($fp, $contents);
109:         fclose($fp);
110:     }
111:     
112:     public function retrieve($groupName, $filename) {
113:         $cacheFile = "{$this->cacheDir}/$groupName/$filename";
114: 
115:         if (file_exists($cacheFile)) {
116:             return file_get_contents($cacheFile);
117:         } else {
118:             return null;
119:         }
120:     }
121: 
122:     public function getCacheAge($groupName, $filename) {
123:         $cacheFile = "$this->cacheDir/$groupName/$filename";
124: 
125:         if (file_exists($cacheFile)) {
126:             return time() - filemtime($cacheFile);
127:         } else {
128:             return null;
129:         }
130:     }
131: 
132:     public function cacheExists($groupName, $filename) {
133:         $cacheFile = "$this->cacheDir/$groupName/$filename";
134: 
135:         return file_exists($cacheFile);
136:     }
137: 
138:     public function remove($groupName, $filename) {
139:         $cacheFile = "$this->cacheDir/$groupName/$filename";
140: 
141:         @unlink($cacheFile);
142:     }
143: 
144:     public function getFilesInGroup($groupName) {
145:         $path = "$this->cacheDir/$groupName/";
146: 
147:         return $this->util->getFilesInDirectory($path);
148:     }
149: 
150:     public function getGroups() {
151:         return $this->util->getDirectoriesInDirectory($this->cacheDir);
152:     }
153: }
154: 
155: class CacheResult {
156:     public $success = false;
157:     public $usedCache = false;
158:     public $oldCache = false;
159:     public $cacheAge = 0;
160:     public $data;
161: }
162: 
Budabot 4 Docs API documentation generated by ApiGen