Currently I'm working on an application and i have a problem. I want to display an html page but the probem is : there is a lot of data/query behind the page. Is it possible to save the html page with the data every morning and then display the html page saved ? I dont want to load the data every time I load the page because the loading is really long.
I'm working with ZendFramwork and Oracle.
You can use either local storage or session storage for this.
HTML web storage provides two objects for storing data on the client:
window.localStorage - stores data with no expiration date
window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)
Use this link to learn more (https://www.w3schools.com/html/html5_webstorage.asp)
You can use GitHub Pages, write a script in any language to send data in GitHub web page on your decided time and all done your html page in dynamic but act as a static and loads in no time
I think you want to use frontend cache.
There are at least 3 versions of Zend Framework, but the caching si very similar.
For Zend 1 there is some theory https://framework.zend.com/manual/1.12/en/zend.cache.theory.html#zend.cache.clean
Best way is set frontend cache in routes
For that, use this in your router definition file
addRoute($router, [
'url' => "[your-path]",
'defaults' => [
'controller' => '[controller-name]',
'action' => '[action-name]',
'cache' => [TIME-OF-CACHE] // 2 hours = 7200
]
]);
Then, if you really want to delete this cache every morning, you should do it manually, by some CRON script.
For that, try to use this
Zend Framework Clearing Cache
Here is the solution:
You need a cron job that runs the script (the HTML file) every morning
Add ob_start() to beginning of your HTML file
Save the buffer into a file :)
<?php
ob_start();
// Display that HTML file here. You don't need to change anything.
// Add this to the end of your file to output everything into a file.
$out = ob_get_contents();
ob_end_clean();
file_put_contents('cached.html', $out);
?>
Related
i am trying to rewrite my code to support multithreading ,it is a simple code but i can't figure out how to do it,basically what it do is
request the first webpage with curl --> to get a unique id
use the unique id to request another page --> to get a session
use the session to request another page --->sleep() then do it again
now this is what a single thread do,but i want to create a lot of threads in the same time
what i did is ,create 3 sperate files
the first one create 10 sessions and save them in a txt file with other parameters (session1|unique_id1|paramter1|anotherparameter1)
the second file contain this code
$sessions = file('sessions.txt');
$WshShell = new COM("WScript.Shell");
foreach($sessions as $kk => $session) {
if (!empty($session)) {
$oExec = $WshShell - > Run("php requests.php $kk", 0, false);
}
}
it open the txt file,and foreach line it open the requests file with the line number in argv
and in the third file,it take the line number ,and open the sessions file ,retreive the paramater of the session and send requests with that session
so this is how i did my multithreading,but i feel like i wrote a php code with rocks
now i want to rewrite it without having to open 10 sperate php process
There really isn't a native way to do threading in PHP. The approach you took works, but I would approach it differently. It's possible to fork processes in PHP. This I've done and works well.
One approach is to use some messaging system like RabbitMQ and distribute the work that way. Basically an Actor or Pub-sub model.
Another approach that might work well for you would be "pthreads". http://php.net/manual/en/book.pthreads.php
I've not tried this method myself so I cannot give you details as to how well it does or doesn't work.
Hope this helps!
When I forget to translate something, somewhere Project VIEW, I change the file /app/Locale/por/LC_MESSAGES/default.po and sending it back to the server.
But mostly, this 'new translation', takes HOURS to be viewed, in short: I just send the file, cleaned the cache and browser CakePHP, press F5, and ... NOTHING HAPPENS.
For what reason?
[EDIT]
<?php echo $this->Form->input('Item.0.description', array('label' => false,
'class' => 'span12', 'div' => array('class' => 'span7'), 'rows' => 3,
'placeholder' => __('Type the description'))); ?>
To force the language to update you can clear the persistent and models directories in the /tmp/cache directory. If view caching is enabled you'll have to clean out views as well.
Caching is disabled when debug is set to 2 (which is the value for using the framework during development) and the persistent directory is populated with new cache files, overwriting the old ones each time a view is loaded. So the debug switch and subsequent browser refresh might clean the old language files for you.
I found a strange solution:
I set (app/Config/core.php)...
Configure::write('debug', 2); // It was 0
Press, F5... wait... and works.
Later, I back...
Configure::write('debug', 0);
Press F5 again, and works.
Why? I no have idea.
Even if Configure::write('debug', 2); I recommend to delete the remote folder containing the translations, refresh with browser (to state that nothing is translated anymore) then reupload the translations folder.
Works perfectly that way for me.
At first apologies in advance if there is some problem in my ques, i am new to heroku ,my basic problem is i have some messed up code, where i want to test if i am able to fetch facebook variables in my own code and use them..
in my .php file i want to put name for person using my facebook app to displayname array variable of array and url of array should get the application users picture..i took the idea to assign these value via the index.php file provided by facebook itself.
My .php file code is :-
$basic = $facebook->api('/me');
$options = array(
'displayName' => he(idx($basic, 'name')),
'image' => array(
'url' => 'https://graph.facebook.com/'.he($id).'/picture?type=square',
'height => '48',
'width => '48'
)
);
but there is something wrong going here which i cant figure out.
i tried to debug it via javascript or other techniques but now able to connect the .php file to some .js file by any means to transfer variable values present in .php file and print them on my browser,i use to edit code at my own system and push it via git and since the code is executed at heroku i cant figure out what errors are creping in..i am using free account as per now so is there any way i can see my code in execution at heroku.. or any help to debug my code efficiently..
Edit1: alternatively is there any way i can pass these variable from my .PHP file to a separate .JS file and print variables in message box or something..any example code given will help a lot..there are many questions asked in this regard to transfer variables from separate .PHP file to separate .JS file..but i found no direct answer for it, all suggest workarounds but no direct way... questions i visited for it are ..
What's the best way to pass a PHP variable to Javascript?
Grab/input php variable in javascript?
and some more but dint find the perfect answer.
Edit2: if my ques needs more info plz let me know,and if second option is the choice left to debug my code ..then can someone give me an example with transferring variable/array present in testfile.php file say present at appfolder/php/lib/testfile.php and output it on browser in HTML format using testjs.js file say present at appfolder/lib/js/testjs.js
A common exchange format between PHP (or other language) and JavaScript is JSON. You can encode an array (or an php object) to json using json_encode, in PHP. Like this :
$options_json = json_encode($options);
So, you can write this javascript variable in your html results, like this :
echo '<script>var options = ', json_encode($options), ';</script>';
Your picture will then be accessible using javascript :
console.log(options);
console.log(options.url);
Is it possible to cache only a specific part of a page in PHP, or the output of a specific section of code in the PHP script? It seems when I try to cache a particular page, it caches the whole page which is not want I want, some of the content in my page should be updated with every page load while others (such as a dropdown list with data from a database) only needs to be updated every hour or so.
If you are talking about caching by the browser (and any proxies it might interact with), then no. Caching only takes place on complete HTTP resources (i.e. on a per URI basis).
Within your own application, you can cache data so you don't need to (for example) hit the database on every request. Memcached is a popular way to do this.
Zend_Cache
I would probably use Zend Frameworks Zend_Cache library for this.
You can just use this component without needing to use the entire framework.
Step over to Zend Framework Download Page and grab the latest.
After you have downloaded the core files, you will need to include Zend_Cache in your project.
Zend_Cache docs.
Have you decided how you want to cache your data? Are you using a file system? Or are you memcache? Once you know which you are going to use, you need to use a specific Zend_Cache backend.
Zend_Cache Backends / Zend_Cache Frontends
You need to use a backend (how you are caching in storage what it is you want to cache) and
You need to use a frontend (how do you actually want to cache.. like using a buffer, or caching function results etc)
Backend documentation: Zend_Cache Backends
Frontend documentation: Zend_Cache Frontends
So you would do something like this...
<?php
// configure caching backend strategy
$backend = new Zend_Cache_Backend_Memcached(
array(
'servers' => array( array(
'host' => '127.0.0.1',
'port' => '11211'
) ),
'compression' => true
) );
// configure caching frontend strategy
$frontend = new Zend_Cache_Frontend_Output(
array(
'caching' => true,
'cache_id_prefix' => 'myApp',
'write_control' => true,
'automatic_serialization' => true,
'ignore_user_abort' => true
) );
// build a caching object
$cache = Zend_Cache::factory( $frontend, $backend );
This would create a cache which makes use of the Zend_Cache_Frontend_Output caching mechanisms.
To use Zend_Cache_Frontend_Output which is want you want, it would be simple. Instead of the core you would use output. The options which you pass are identical. Then to use it you would:
Zend_Cache_Frontend_Output - Usage
// if it is a cache miss, output buffering is triggered
if (!($cache->start('mypage'))) {
// output everything as usual
echo 'Hello world! ';
echo 'This is cached ('.time().') ';
$cache->end(); // output buffering ends
}
echo 'This is never cached ('.time().').';
Useful Blog: http://perevodik.net/en/posts/14/
Sorry this question took longer to write than expected and lots of answers have been written I see!
You could roll your own caching with ob_start(), ob_end_flush() and similar functions. Gather the desired output, dump it into some file or database, and read later if conditions are the same. I usually build md5 sum of the state and restore it later.
It depends on both what caching and view technologies are you using. Generally speaking yes, you can do something like this:
// if it is a cache miss, output buffering is triggered
if (!($cache->start('mypage'))) {
// output everything as usual
echo 'Hello world! ';
echo 'This is cached ('.time().') ';
$cache->end(); // output buffering ends
}
echo 'This is never cached ('.time().').';
taken from Zend_Cache documentation.
Otherwise in your example you can always make a function which returns the dropdown list and implement the cache mechanism inside that function. In this way your page is not even aware of caching.
Do you happen to know any good rss generator script with caching function. All the script I have found over the net so far doesn't support caching! I need the the content of rss to be generated automatically from database in a specified period of time.
Thanks in advance
First, to add caching to the script, it seems like it wouldn't be too hard to put Zend_Feed and Zend_Cache together - or just wrap your current generation script with Zend_Cache.
Just setup the cache with your lifetime:
$frontendOptions = array(
'lifetime' => 7200, // cache lifetime of 2 hours
'automatic_serialization' => true
);
Then check if the cache is still valid:
if(!$feed = $cache->load('myfeed')) {
//generate feed
$cache->save($feed, 'myfeed');
}
//output $feed
I don't know how you form your RSS, but you can import an array structure to Zend_Feed:
$rssFeedFromArray = Zend_Feed::importArray($array, 'rss');
Of course the best way may be to just use your current feed generator and save the output to a file. Use that file as the RSS feed, then use cron/web hooks/queue/whatever to generate the static file. That would be simpler, and use less resources, than having the generation script do the caching.
//feedGen.php
//may require some output buffering if the feed generator outputs directly
$output = $myFeedGenerator->output();
file_put_contents('feed.rss', $output);
Now the feed link is /feed.rss, and you just run feedGen.php whenever it needs to be refreshed. Serving the static file (not even parsed by php) means less for your server to do.