Symfony cache dont work with class/object - php

I use some php library, and generate element of class
$elnew = new LibClass();
I want to save this variable to cache.
If I make like this
$elem = $cache->getItem($ig_name);
if (!$elem->isHit()) {
$elem->set($elnew);
$cache->save($ig);
}
$elem->isHit() is always false. I checked how cache works with string - all is ok.
Also I'm not able to serialyze/unserialyze this object because it says
Serialization of 'Closure' is not allowed
and no way to modify LibClass
How can I save $elnew to cache? Any variants for with symfony components? Or maybe other libs can help me?

Serialization of 'Closure' is not allowed
You can use the PHP SuperClosure library to get rid of this.
Also you can try other memory storages like Redis or Memcache to cache your objects. See this resolved stackoverflow question.

Related

What is idiomatic way to clear whole cache in Twig since 1.22.x?

Since 1.22.0 version of Twig, method Twig_Environment::clearCacheFiles() is deprecated. However, I didn't found anywhere (nor in documentation, nor in repo tickets, nor here on SO) something which can replace this deprecated method.
So what is the idiomatic (and not deprecated) way to clear all cache files now?
Clearing those files via implementing own function seems to be pretty weird.
So there is no new idiomatic way to clear all cache files.
Twig project maintainers leave it for library users on their own. The arguments are:
The argument was that Twig itself will only support filesystem cache (due to opcache). Thus clear() would be the same as removing the cache folder manually. Thus it doesn't need to be in the interface. So you have to clear your cache either manually or write the method yourself.
More details may be read in relevant issue early referred by #alain-tiemblo in comments.
Here is the deprecated method from Twig made into a generalized function if you are interested. It only removes leaf nodes (the php files that Twig generates) but leaves the directories. You will want to make sure you pass in the correct path.
function clearCacheFiles($cacheLocation) {
if (is_string($cacheLocation)) {
foreach (new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($cacheLocation),
\RecursiveIteratorIterator::LEAVES_ONLY) as $file
) {
if ($file->isFile()) {
#unlink($file->getPathname());
}
}
}
}
From Twig/lib/Twig/Environment.php as it appeared when first marked deprecated.

PHP: Object serialization working via direct file handling but not via session

I'm trying to serialize and unserialize a quite long object -- 250KB compressed -- via session but it's not working.
I've done two tests. The first one consisted on directly serialize and unserialize the object more than once for checking if the problem was the serialization per se but everything ran ok. The second one consisted on writing the serialized object into a file and that worked fine too.
Unfortunately it would be insane to post here or elsewhere all the code itself.
Has anyone dealed with a problem like that or suggest any other test to be done?
have you make a instance of unserialize first?
eg
$a = new A;
if($_SESSION['my_a']) {
$a = unserialize($_SESSION['my_a']);
}
Go into php.ini at: ...\apache\Apache2.4.4\bin
change: upload_max_filesize
The problem was classes with no support to serialization like Zend_Db_Table and Zend_Db_Adapter_Abstract subclasses.
When serializing objects it's needed to go as deep as possible to map every object dependency and treat them and I ended up by giving up serialize what motivated me to post the question in first place.
Thank everyone who tried helping me on this.

Doctrine Cache Symfony2 deleteByPrefix

I would like to delete all my cache entries prefixed by a string, but my code returns :
Fatal error: Call to undefined method Doctrine\Common\Cache\FilesystemCache::deleteByPrefix()
and my code is
$deleted = $cachemanager->deleteByPrefix('catalog_');
like where I checked : into the documentation of doctrine http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/caching.html#deleting.
I need to clear the cache since controller, not from CLI...
Does someone as an alternative to this method 'deleteByPrefix()' ?
You can use the namespace. This will require reorganizing your caches. Or you can write your own implementation (extending Doctrine\Common\Cache\FilesystemCache) which will implement deleteByPrefix(), but you'll use the ability to simply switch cache provider.
Finally, I come to use the Zend Framework cache bundle, who permit to delete by prefix...

Reloading a Class

I have a PHP daemon script running on the command line that can be connected to via telnet etc and be fed commands.
What it does with the command is based on what modules are loaded, which is currently done at the start. (psuedocode below for brevity)
$modules = LoadModules();
StartConnection();
while(true){
ListenForCommands();
}
function LoadModules(){
$modules = Array();
$dir = scandir("modules");
foreach($dir as $folder){
include("modules/".$folder."/".$folder.".php");
$modules[$folder] = new $folder;
}
}
function ListenForCommands(){
if(($command = GetData())!==false){
if(isset($modules[$command])){
$modules[$command]->run();
}
}
}
So, an example module called "bustimes" would be a class called bustimes, living in /modules/bustimes/bustimes.php
This works fine. However, I'd like to make it so modules can be updated on the fly, so as part of ListenForCommands it looks at the filemtime of the module, works out if it's changed, and if so, effectively reloads the class.
This is where the problem comes in, obviously if I include the class file again, it'll error as the class already exists.
All of the ideas I have of how to get around this problem so far are pretty sick and I'd like to avoid doing.
I have a few potential solutions so far, but I'm happy with none of them.
when a module updates, make it in a new namespace and point the reference there
I don't like this option, nor am I sure it can be done (as if I'm right, namespaces have to be defined at the top of the file? That's definitely workaroundable with a file_get_contents(), but I'd prefer to avoid it)
Parsing the PHP file then using runkit-method-redefine to redefine all of the methods.
Anything that involves that kind of parsing is a bad plan.
Instead of including the file, make a copy of the file with everything the same but str_replacing the class name to something with a rand() on the end or similar to make it unique.
Does anyone have any better ideas about how to either a) get around this problem or b) restructure the module system so this problem doesn't occur?
Any advice/ideas/constructive criticism would be extremely welcome!
You should probably load the files on demand in a forked process.
You receive a request
=> fork the main process, include the module and run it.
This will also allow you to run several commands at once, instead of having to wait for each one to run before launching the next.
Fork in php :
http://php.net/manual/en/function.pcntl-fork.php
Tricks with namespaces will fail if module uses external classes (with relative paths in namespace).
Trick with parsing is very dangerous - what if module should keep state? What if not only methods changed, but, for example, name of implemented interface? How it will affect other objects if they have link to instance of reloaded class?
I think #Kethryweryn is something you can try.

Problems using a Wiki to HTML script (FFNN) as Helper in CodeIgniter

I've found this script for converting wiki syntax to HTML in php and i've tried to integrate it into Codeigniter. it seems really easy to use. However, it's not working, and instead producing around 8 of these errors:
Message: Use of undefined constant LS_NONE - assumed 'LS_NONE'
I think this is because Codeigniter helpers are not a class but rather functions, and this bit of code is a class, or does this issue lie with something else? I've also tried to use it as a model without success.
It also seems horribly outdated (2007). Could somebody suggest a really simple alternative or maybe give an idea of how to convert this to a simple function if that is possible? It's a very short piece of code. I'm not sure how these constants work in relationship to a function versus a class.
I've given the Text_Wiki from Pear ago, but the use and complexity well exceeds both my requirements and knowledge :)
//Any help would be greatly appreciated
Loaded using:
$row = $query->row();
$content=$row->course_content;
$this->load->helper('wiki');
$content=explode("\n", $content);
$output = WikiTextToHTML::convertWikiTextToHTML($content);
$html=array_merge($output);
$data['contents'][]= $html;
$this->load->view('default/a',$data);
It looks like the script is actually a class. put it in the libraries folder and load it with $this->load->library(). That will allow it to properly initialize and define the constants that it uses.
something like:
$this->load->library('wikitexttohtml');
$this->wikitexttohtml->convertWikiTextToHTML($wiki_text);

Categories