Permalink
Browse files

init sources

  • Loading branch information...
1 parent 18a2395 commit 64bbb5eb447e38eb5cb0ae59e344d917b6e7b9f9 @kelu95 committed Jun 15, 2011
Showing with 276 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +25 −0 ApcBundle.php
  3. +87 −0 Command/ClearCommand.php
  4. +139 −0 Controller/Cache.php
  5. +19 −0 LICENSE
View
@@ -0,0 +1,6 @@
+/.settings/
+.buildpath
+.project
+.DS_Store
+[Tt]humbs.db
+*.DS_Store
View
@@ -0,0 +1,25 @@
+<?php
+
+namespace Allblacks\ApcBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class ApcBundle extends Bundle
+{
+ /**
+ * {@inheritdoc}
+ */
+ public function getNamespace()
+ {
+ return __NAMESPACE__;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getPath()
+ {
+ return strtr(__DIR__, '\\', '/');
+ }
+
+}
View
@@ -0,0 +1,87 @@
+<?php
+/*
+* This file is part of the Kelu\UserBundle
+*
+* Copyright (c) 2011 Luc Vandesype - http://www.neoplug.com
+*/
+
+namespace Allblacks\ApcBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\Command as BaseCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Output\Output;
+
+/**
+* ClearCommand.
+*
+* Lets you clear APC cache user or filehits
+*/
+class ClearCommand extends BaseCommand
+{
+ /**
+ * @see Command
+ */
+ protected function configure()
+ {
+ $this
+ ->setName('apc:clear')
+ ->setDescription('Clear all APC cache user or filehits')
+ ->setDefinition(array(
+ new InputArgument('type', InputArgument::REQUIRED, 'The cache type (user or file)')
+ ))
+ ->setHelp(<<<EOT
+The <info>apc:clear</info> command that clear all APC cache :
+<info>php app/console apc:clear type</info>
+The type argument is required
+EOT
+ );
+ }
+
+ /**
+ * @see Command
+ */
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $logger = $this->container->get('logger'); //get logger service
+
+ $cache_type=''; //empty for filehits
+ if($input->getArgument('type')=='user') $cache_type='user';
+
+ $cache = $this->container->get('apc_cache'); //get apc_cache service
+ $cache->clearAll($cache_type);
+
+ $output_str='APC clear all cache '.$input->getArgument('type').' from HOST : '.gethostname();
+
+ $logger->info($output_str);
+ $output->writeln($output_str);
+ }
+
+ /**
+ * @see Command
+ */
+ protected function interact(InputInterface $input, OutputInterface $output)
+ {
+ $logger = $this->container->get('logger');
+
+ if (!$input->getArgument('type')) {
+ $type = $this->getHelper('dialog')->askAndValidate(
+ $output,
+ 'Please specify the cache to clear [user or file]:',
+ function($type)
+ {
+ if (empty($type) || ($type!='user' && $type!='file')) {
+ throw new \Exception('Cache type must be user our file');
+ }
+
+ return $type;
+ }
+ );
+ $input->setArgument('type', $type);
+ }
+ }
+
+}
+
View
@@ -0,0 +1,139 @@
+<?php
+
+namespace Allblacks\ApcBundle\Controller;
+
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+
+/**
+* Service to manage your cache APC
+ *
+ * Call example :
+ *
+ * $cache = $this->get('apc_cache');
+ * if(!$cache->exist('my_cache_var')){
+ * $cache->add('my_cache_value', [ int $ttl ]);
+ * }
+ * echo $cache->get();
+ */
+
+class Cache
+{
+ protected $em; //entity manager service
+ protected $apc_enabled; //conf parameter value
+ protected $apc_ttl; //conf parameter value
+ protected $cache_name; //var cache name for instance
+
+ /**
+ * @param object $em (entity manager service)
+ * @param int $apc_enabled (conf parameter value)
+ * @param int $apc_ttl (conf parameter default time to leave)
+ */
+ public function __construct($em,$apc_enabled,$apc_ttl)
+ {
+ $this->em=$em->getEntityManager();
+ $this->apc_enabled=$apc_enabled;
+ $this->apc_ttl=$apc_ttl;
+
+ $this->isApcEnabled(); //check if apc is enabled and correctly configured
+ }
+
+ /**
+ * Test if a cache user var exist
+ *
+ * @param string $name
+ * @return true or false
+ */
+ public function exist($name)
+ {
+ $this->cache_name=$name;
+ if (apc_exists($name)) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Set a cache user var name, not really used but can be helpfull
+ *
+ * @param string $name
+ */
+ public function setName($name)
+ {
+ $this->cache_name=$name;
+ }
+
+ /**
+ * Add a cache user var
+ *
+ * @param mixed $value
+ * @param int $ttl or empty : cache time to leave in seconds. If empty get the conf parameter ttl value
+ * @return string cache variable value or false
+ */
+ public function add($value,$ttl='')
+ {
+ if(empty($this->cache_name) || empty($value)){
+ throw new \Exception("The APC Cache need a cache name and cache value");
+ return false;
+ }
+ if($ttl=='') $ttl=$this->apc_ttl;
+ apc_add($this->cache_name, $value,$ttl);
+ return $this->cache_name;
+ }
+
+ /**
+ * Get a cache user var
+ *
+ * @param string $name variable name, can be empty, so get the pre defined cache_name var
+ * @return string cache variable value
+ */
+ public function get($name='')
+ {
+ if($name=='') $name=$this->cache_name;
+ return apc_fetch($name);
+ }
+
+ /**
+ * Delete cache user var
+ *
+ * @param string $name variable name
+ */
+ public function delete($name)
+ {
+ apc_delete($name);
+ }
+
+ /**
+ * Delete cache user or filehits
+ *
+ * @param string $cache_type user or empty
+ */
+ public function clearAll($cache_type='')
+ {
+ apc_clear_cache($cache_type);
+ }
+
+ /**
+ * Get apc cache infos by type (user or filehits)
+ *
+ * @param string $cache_type user or empty
+ * @return array
+ */
+ public function showInfos($cache_type='user')
+ {
+ return apc_cache_info($cache_type);
+ }
+
+ /**
+ * Check if apc is enabled
+ *
+ * @return true or false
+ */
+ private function isApcEnabled()
+ {
+ if(function_exists('apc_add') && $this->apc_enabled==1){
+ return true;
+ }
+ throw new \Exception("The APC Extension is not enabled.\nPlease, check your server configuration.\nThe parameter \"apc_enabled\" must set to true\nThe parameter \"apc_ttl\" must be int val");
+ return false;
+ }
+}
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2011 Luc Vandesype - http://www.neoplug.com
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

0 comments on commit 64bbb5e

Please sign in to comment.