Skip to content

Commit

Permalink
performance fixes - catched on production
Browse files Browse the repository at this point in the history
  • Loading branch information
ahilles107 committed Mar 25, 2015
1 parent b736d2c commit 83706ed
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 70 deletions.
2 changes: 2 additions & 0 deletions newscoop/application/configs/parameters/parameters.yml
Expand Up @@ -21,6 +21,8 @@ parameters:
view: ""
modules:
- ""
session:
throw_startup_exceptions: false
pluginPaths:
Resource: "%application_path%/../library/Resource"
session:
Expand Down
Expand Up @@ -26,7 +26,7 @@ function smarty_block_list_slideshow_items($params, $content, &$smarty, &$repeat
$cacheService = \Zend_Registry::get('container')->get('newscoop.cache');

if (!isset($content)) { // init
$start = $context->next_list_start('Newscoop\TemplateList\SlideshowItemssList');
$start = $context->next_list_start('Newscoop\TemplateList\SlideshowItemsList');
$list = new \Newscoop\TemplateList\SlideshowItemsList(
new \Newscoop\Criteria\SlideshowItemCriteria(),
$paginatorService,
Expand Down
7 changes: 6 additions & 1 deletion newscoop/include/smarty/campsite_plugins/function.count.php
Expand Up @@ -54,6 +54,11 @@ function smarty_function_count($p_params, &$p_smarty)
$art_language_code = $art_language_obj->getCode();
}

$count_automatically = true;
if (isset($p_params['dont_count_automatically'])) {
$count_automatically = false;
}

if ((!$art_number) || (!$art_language_num)) {
$meta_article = $campsite->article;
if ($meta_article->defined) {
Expand Down Expand Up @@ -108,7 +113,7 @@ function smarty_function_count($p_params, &$p_smarty)
$article_number = $article->getProperty('Number');
$name_spec = '_' . $article_number . '_' . $art_language_code;

$content .= \Statistics::JavaScriptTrigger(array('name_spec' => $name_spec, 'object_type_id' => $object_type_id, 'request_object_id' => $requestObjectId));
$content .= \Statistics::JavaScriptTrigger(array('count_automatically' => $count_automatically,'name_spec' => $name_spec, 'object_type_id' => $object_type_id, 'request_object_id' => $requestObjectId));
}
} catch (\Exception $ex) {
return '';
Expand Down
127 changes: 68 additions & 59 deletions newscoop/library/Newscoop/Services/CacheService.php
Expand Up @@ -18,7 +18,12 @@ class CacheService
*
* @var \Doctrine\Common\Cache\CacheProvider
*/
protected $cacheDriver;
protected $cacheDriver = null;

/**
* @var \Newscoop\NewscoopBundle\Services\SystemPreferencesService
*/
protected $systemPreferences;

/**
* Initialize cache driver (based on system preferences settings, default is array)
Expand All @@ -27,56 +32,7 @@ class CacheService
*/
public function __construct($systemPreferences)
{
if (php_sapi_name() === 'cli') {
$this->cacheDriver = new \Doctrine\Common\Cache\ArrayCache();

return;
}

try {
switch ($systemPreferences->get('DBCacheEngine', 'Array')) {
case 'apc':
$this->cacheDriver = new \Doctrine\Common\Cache\ApcCache();
break;
case 'memcache':
$memcache = new \Memcache();
$memcache->connect(
$systemPreferences->get('DBCacheEngineHost', '127.0.0.1'),
$systemPreferences->get('DBCacheEnginePort', '11211')
);

$this->cacheDriver = new \Doctrine\Common\Cache\MemcacheCache();
$this->cacheDriver->setMemcache($memcache);
break;
case 'memcached':
$memcached = new \Memcached();
$memcached->addServer(
$systemPreferences->get('DBCacheEngineHost', '127.0.0.1'),
$systemPreferences->get('DBCacheEnginePort', '11211')
);

$this->cacheDriver = new \Doctrine\Common\Cache\MemcachedCache();
$this->cacheDriver->setMemcached($memcached);
break;
case 'xcache':
$this->cacheDriver = new \Doctrine\Common\Cache\XcacheCache();
break;
case 'redis':
$redis = new \Redis();
$redis->connect(
$systemPreferences->get('DBCacheEngineHost', '127.0.0.1'),
$systemPreferences->get('DBCacheEnginePort', '6379')
);
$this->cacheDriver = new \Doctrine\Common\Cache\RedisCache();
$this->cacheDriver->setRedis($redis);
break;
default:
$this->cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
break;
}
} catch (\Exception $e) {
$this->cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
}
$this->systemPreferences = $systemPreferences;
}

/**
Expand All @@ -88,7 +44,7 @@ public function __construct($systemPreferences)
*/
public function fetch($id)
{
return $this->cacheDriver->fetch($this->getCacheKey($id));
return $this->getCacheDriver()->fetch($this->getCacheKey($id));
}

/**
Expand All @@ -100,7 +56,7 @@ public function fetch($id)
*/
public function contains($id)
{
return $this->cacheDriver->contains($this->getCacheKey($id));
return $this->getCacheDriver()->contains($this->getCacheKey($id));
}

/**
Expand All @@ -114,7 +70,7 @@ public function contains($id)
*/
public function save($id, $data, $lifeTime = 1400)
{
return $this->cacheDriver->save($this->getCacheKey($id), $data, $lifeTime);
return $this->getCacheDriver()->save($this->getCacheKey($id), $data, $lifeTime);
}

/**
Expand All @@ -126,7 +82,7 @@ public function save($id, $data, $lifeTime = 1400)
*/
public function delete($id)
{
return $this->cacheDriver->delete($this->getCacheKey($id));
return $this->getCacheDriver()->delete($this->getCacheKey($id));
}

public function getCacheKey($id, $namespace = null)
Expand All @@ -146,19 +102,19 @@ public function getCacheKey($id, $namespace = null)

public function getNamespace($namespace)
{
if ($this->cacheDriver->contains($namespace)) {
return $this->cacheDriver->fetch($namespace);
if ($this->getCacheDriver()->contains($namespace)) {
return $this->getCacheDriver()->fetch($namespace);
}

$value = $namespace .'|'.time();
$this->cacheDriver->save($namespace, $value);
$this->getCacheDriver()->save($namespace, $value);

return $value;
}

public function clearNamespace($namespace)
{
$this->cacheDriver->save($namespace, time());
$this->getCacheDriver()->save($namespace, time());
}

/**
Expand Down Expand Up @@ -200,6 +156,59 @@ public function getAvailableCacheEngines()
*/
public function getCacheDriver()
{
if (!is_null($this->cacheDriver)) {
return $this->cacheDriver;
}

if (php_sapi_name() === 'cli') {
return $this->cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
}

try {
switch ($this->systemPreferences->get('DBCacheEngine', 'Array')) {
case 'apc':
$this->cacheDriver = new \Doctrine\Common\Cache\ApcCache();
break;
case 'memcache':
$memcache = new \Memcache();
$memcache->connect(
$this->systemPreferences->get('DBCacheEngineHost', '127.0.0.1'),
$this->systemPreferences->get('DBCacheEnginePort', '11211')
);

$this->cacheDriver = new \Doctrine\Common\Cache\MemcacheCache();
$this->cacheDriver->setMemcache($memcache);
break;
case 'memcached':
$memcached = new \Memcached();
$memcached->addServer(
$this->systemPreferences->get('DBCacheEngineHost', '127.0.0.1'),
$this->systemPreferences->get('DBCacheEnginePort', '11211')
);

$this->cacheDriver = new \Doctrine\Common\Cache\MemcachedCache();
$this->cacheDriver->setMemcached($memcached);
break;
case 'xcache':
$this->cacheDriver = new \Doctrine\Common\Cache\XcacheCache();
break;
case 'redis':
$redis = new \Redis();
$redis->connect(
$this->systemPreferences->get('DBCacheEngineHost', '127.0.0.1'),
$this->systemPreferences->get('DBCacheEnginePort', '6379')
);
$this->cacheDriver = new \Doctrine\Common\Cache\RedisCache();
$this->cacheDriver->setRedis($redis);
break;
default:
$this->cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
break;
}
} catch (\Exception $e) {
$this->cacheDriver = new \Doctrine\Common\Cache\ArrayCache();
}

return $this->cacheDriver;
}
}
Expand Up @@ -29,9 +29,11 @@ public function onResponse(FilterResponseEvent $event)
return;
}

$response = $event->getResponse();
$responseData = $event->getResponse()->getContent();
$response->setContent(Json::indent($responseData));
$event->setResponse($response);
if (APPLICATION_ENV === 'development' || APPLICATION_ENV === 'dev' || $request->query->get('pretty_json', false) == true) {
$response = $event->getResponse();
$responseData = $event->getResponse()->getContent();
$response->setContent(Json::indent($responseData));
$event->setResponse($response);
}
}
}
Expand Up @@ -30,7 +30,7 @@ public function onRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if (strpos($route, 'newscoop_gimme_') === false) {
if (strpos($route, 'newscoop_gimme_') === false || $route == 'newscoop_get_img') {
return;
}

Expand Down
Expand Up @@ -16,7 +16,7 @@
class ImagesController extends Controller
{
/**
* @Route("get_img")
* @Route("get_img", name="newscoop_get_img")
*/
public function indexAction(Request $request)
{
Expand Down
Expand Up @@ -42,7 +42,7 @@ public function __construct(IssueService $issueService)
public function onRequest(GetResponseEvent $event)
{
$onAdminInterface = strpos($event->getRequest()->getRequestUri(), '/admin');
if ($onAdminInterface === false) {
if ($onAdminInterface === false && $event->getRequest()->get('_route') != 'newscoop_get_img') {
$this->issueService->issueResolver($event->getRequest());
}
}
Expand Down
Expand Up @@ -36,6 +36,12 @@ public function __construct(Translator $translator, $cacheService, $pluginsServi

public function onRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if (strpos($route, 'newscoop_gimme_') === false || $route == 'newscoop_get_img') {
return;
}

$locale = $event->getRequest()->getLocale();
$cacheKey = 'oldPlugins_translations_'.count($this->pluginsService->getEnabledPlugins());
if ($this->cacheService->contains($cacheKey)) {
Expand Down
Expand Up @@ -33,6 +33,8 @@ public function __construct(PublicationService $publicationService)

public function onRequest(GetResponseEvent $event)
{
$this->publicationService->publicationResolver($event->getRequest());
if ($event->getRequest()->get('_route') != 'newscoop_get_img') {
$this->publicationService->publicationResolver($event->getRequest());
}
}
}
2 changes: 1 addition & 1 deletion newscoop/template_engine/classes/CampURIShortNames.php
Expand Up @@ -248,7 +248,7 @@ private function _getPublication()
private function _getLanguage($code, MetaPublication $publication)
{
$cacheService = \Zend_Registry::get('container')->getService('newscoop.cache');
$cacheKey = $cacheService->getCacheKey(array('CampURIShortNameLanguage', $code, serialize($publication)), 'language');
$cacheKey = $cacheService->getCacheKey(array('CampURIShortNameLanguage', $code, $publication->name), 'language');
if ($cacheService->contains($cacheKey)) {
return $cacheService->fetch($cacheKey);
} else {
Expand Down

0 comments on commit 83706ed

Please sign in to comment.