Source of file CachedTwitterService.php
Size: 3,202 Bytes - Last Modified: 2021-12-24T07:11:44+00:00
/var/www/docs.ssmods.com/process/src/src/service/CachedTwitterService.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | <?php namespace XD\Twitter\Services; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injector; /** * Caches a wrapped twitter service * * @author Damian Mooyman * * @package twitter */ class CachedTwitterService implements ITwitterService { use Configurable; /** * @var ITwitterService */ protected $cachedService = null; private static $lifetime = 300; public function __construct(ITwitterService $service = null) { $this->cachedService = $service; } public function getTweets($user, $count) { // Init caching $cacheKey = "getTweets_{$user}_{$count}"; $cache = Injector::inst()->get(CacheInterface::class . '.cachedTwitterService'); // Return cached value, if available if ($rawResult = $cache->get($cacheKey)) { return unserialize($rawResult); } // Save and return $result = $this->cachedService->getTweets($user, $count); $cache->set($cacheKey, serialize($result), Config::inst()->get(cachedTwitterService::class, 'lifetime')); return $result; } public function getList($listID, $count) { // Init caching $cacheKey = "getList_{$listID}_{$count}"; $cache = Injector::inst()->get(CacheInterface::class . '.cachedTwitterService'); // Return cached value, if available if ($rawResult = $cache->get($cacheKey)) { return unserialize($rawResult); } // Save and return $result = $this->cachedService->getList($listID, $count); $cache->set($cacheKey, serialize($result), Config::inst()->get(cachedTwitterService::class, 'lifetime')); return $result; } /** * get favourite tweets associated with the user. * */ public function getFavorites($user, $count) { // Init caching $cacheKey = "getFavorites_{$user}_{$count}"; $cache = Injector::inst()->get(CacheInterface::class . '.cachedTwitterService'); // Return cached value, if available if ($rawResult = $cache->get($cacheKey)) { return unserialize($rawResult); } // Save and return $result = $this->cachedService->getFavorites($user, $count); $cache->set($cacheKey, serialize($result), Config::inst()->get(cachedTwitterService::class, 'lifetime')); return $result; } public function searchTweets($query, $count) { // Init caching $cacheKey = "searchTweets_" . str_replace("-", "_", Convert::raw2url($query)) . "_{$count}"; $cache = Injector::inst()->get(CacheInterface::class . '.cachedTwitterService'); // Return cached value, if available if ($rawResult = $cache->get($cacheKey)) { return unserialize($rawResult); } // Save and return $result = $this->cachedService->searchTweets($query, $count); $cache->set($cacheKey, serialize($result), Config::inst()->get(cachedTwitterService::class, 'lifetime')); return $result; } } |