How to use the cache technology? For example, my index. php page is as follows:
Include ("IncDB. php ");
Session_start ();
$ Result = mysql_query ("SELECT * FROM userlinks where id = 'a02' and userid = '". $ _ SESSION ['id']. "'Order by datetime desc limit 1", $ link );
$ Row = mysql_fetch_row ($ result );
Echo "$ row [1]";
Echo"
";
Echo "$ row [2]";
Mysql_close ($ link );
?>
Every time a user logs on, he/she reads data from the database and then displays the data, which is slow. Is there any way, such as caching technology? when the information remains unchanged, it can be directly retrieved from a page without having to query the database every time and then display it again. Thank you.
Reply to discussion (solution)
I can use the cache technology on the Internet, but I still don't understand how to do it? Please advise. Thank you!
Yes, cache can be used.
But what is your cache policy?
Cache location
How to know the data has changed
Have you thought about this?
I have never thought about it. Because the above page will vary according to different users and the content will change, I wonder if it will not work if I put the cache on the website. Do I have to create a cache backup for each user? should I use it on the client? Can I check the data in the background when the data changes, but the latest information can be displayed when the webpage is actively refreshed?
My requirement is to find a way for the user to open this page without reading data from the database every time. This is really slow because the page extracts a lot of data.
Can you help me find out how to do this? Thank you!
However, if the page changes, either the page is changed during background management or the content changes, which may take several months and can be ignored. The other is, the user uses EDIT. modify the data in userlinks on the php page. at this time, the content will change.
In addition, can I cache a page, such as EDIT. php, to the client? I found that every time a user opens the interface, it is very slow.
I have no idea.
You can store SQL query results in the cache.
File cache.
$expire){ $result=mysql_query("SELECT * FROM userlinks where id='a02' and userid=‘".$_SESSION['id']."’ order by datetime desc limit 1",$link); $row = mysql_fetch_row($result); $cache_data = json_encode(array('data'=>$row,'expire'=>time()));}else{ $row = $cache_data['data'];}echo "$row[1]"; echo"
"; echo "$row[2]";mysql_close($link); ?>
Thank you. Sorry, I am very watery. I haven't understood the meaning of this code for a long time. can you help me explain it? In addition, the code on the 6th floor is what the 5th floor says. store the SQL query results and store them in the cache?
I think that if we can store the queried data locally, and then each time we read the data locally, we can implement what I said.
I guess, it seems that the code sets an update time for the cache, and the cache reads data within the time; otherwise, the database is used. If this is the case, it is incorrect, because the user will modify a content multiple times at any time on the edit. php page, but it may not be changed once in a few months. Therefore, if the specified update time for the cache is invalid. Should I add the updated cache code on the edit page, while the index page can directly read the local cache? If not, read the database?
By the way, the code I pasted is incorrect. the extracted data is multiple rows without limit 1. Sorry.
We recommend that you use memcache to cache the SQL query results in the memory of the server. if php updates the data, it will clear the cache in memcache and then index. when php queries the cache, if (the memcache cache does not exist) {update data using SQL statements }.
Sorry, Big Brother. I won't. how can I use memcache? I have seen on the Internet that this is used. is it to add a statement on the edit page? Or others? I don't know anything. Sorry, could you tell me what the code is and where it is written? Forgive me for my ignorance.
Index. php:
Include ("IncDB. php "); session_start (); // use the memcache object $ memcache = new Memcache; $ memcache-> connect ('memcache host address ', 11211 ); // The default memcache port number is 201711if (! $ Memcache-> get ('results') {$ result = mysql_query ("SELECT * FROM userlinks where id = 'a02' and userid = '". $ _ SESSION ['id']. "'Order by datetime desc", $ link); $ row = mysql_fetch_row ($ result); echo "$ row [1]"; echo"
"; Echo" $ row [2] "; mysql_close ($ link); $ memcache-> set ('results', $ row );}
Edit. php:
// In edit. php, if the user updates the data, the memcache cache is cleared. // The user updates the data code ..... $ memcache = new Memcache; $ memcache-> connect ('memcache host address', 11211); // the default port number of memcache is 11211 $ memcache-> delete ('results ', 0); // 0 indicates that the element is deleted immediately.
Since it may not be changed once in a few months, it is enough to directly generate and use static pages. why cache?
Thank you. I will try again. Thank you!
Xuzuning is a back-end change, only once in a few months, but the frequency of user change may be high. Thank you for your attention.
I asked misakaqunianx. I don't understand. 1. based on the method you provided, I extracted 10 data records, for example, and cached the 10 data records. if the user subsequently changed one of the 10 data records, for example, 5th data records, can I use $ memcache on the EDIT page to precisely update this data?
2. on the index page, which statement is used to extract data from the local cache? I cannot understand.
3. if a user has two local user names, will they not be affected?
Sorry, the question is too weak.
Thank you!
I asked misakaqunianx. I don't understand. 1. based on the method you provided, I extracted 10 data records, for example, and cached the 10 data records. if the user subsequently changed one of the 10 data records, for example, 5th data records, can I use $ memcache on the EDIT page to precisely update this data?
2. on the index page, which statement is used to extract data from the local cache? I cannot understand.
3. if a user has two local user names, will they not be affected?
Sorry, the question is too weak.
As long as your user modifies any data, you can clear the memcache cache so that no matter what you modify, you can use SQL to query the database again.
$ Memcache-> get ('results') is used to retrieve the cache from the memory (memcache is not a file cache, but cached in the server memory)
If you want to use memcache, you need to enable the memcache extension. Baidu is the method.
If I upload files to a space, I don't need to add the code you mentioned? Enable the extension again. The service provider should have enabled it. thank you.
If I upload files to a space, I don't need to add the code you mentioned? Enable the extension again. The service provider should have enabled it. thank you.
Generally, service providers are enabled.
Thank you. I will try it and paste it. thank you.
It doesn't seem to work. after I add it, nothing will be displayed on the page. I have a few questions:
1. according to my understanding, the logic is to first determine whether the cache contains data. If yes, extract data from the cache; if not, extract data from the database and ECHO, then $ memcache-> set ('result', $ row); but your code, I can't understand this idea, without if else, only if.
2. if you need to extract data from my page several times, for example, $ result = mysql_query ..., $ result1 = mysql_query ..., do I have to add this code every time I extract data? Or a page. you only need to set it once, and then get ('results'), get ('results1'), get ('results2?
3. $ memcache = new Memcache;
$ Memcache-> connect ('memcache host address', 11211); // The default memcache port number is 11211.
Can I add this sentence to IncDB. php (this is the file set by the linked database) for unified calling?
Thank you for your advice.
Can you teach me? Thank you.
This .......
Excuse me, please take a moment...
It doesn't seem to work. after I add it, nothing will be displayed on the page. I have a few questions:
1. according to my understanding, the logic is to first determine whether the cache contains data. If yes, extract data from the cache; if not, extract data from the database and ECHO, then $ memcache-> set ('result', $ row); but your code, I can't understand this idea, without if else, only if.
2. if you need to extract data from my page several times, for example, $ result = mysql_query ..., $ result1 = mysql_query ..., do I have to add this code every time I extract data? Or a page. you only need to set it once, and then get ('results'), get ('results1'), get ('results2?
3. $ memcache = new Memcache;
$ Memcache-> connect ('memcache host address', 11211); // The default memcache port number is 11211.
Can I add this sentence to IncDB. php (this is the file set by the linked database) for unified calling?
Thank you for your advice.
1. I forgot to write else {$ row = $ memcache-> get ('results ');}
2. add the code to determine the cache each time data is extracted.
3. you do not need to put it in the database configuration. memcache is a cache extension and can be placed in the system configuration.
Hey, you just need to add else {$ row = $ memcache-> get ('results') ;}at the end of the code? What is system configuration? This page is mine. if you do not set the code ($ memcache = new Memcache;
$ Memcache-> connect ('memcache host address ', 11211);) is called in a file, such as IncDB. php. I have to write it again every time. I do not have a dedicated system configuration page.
To use memcache, you must install the memcached server and the php_memcache extension corresponding to the php version.
For global use
$ Memcache = new Memcache;
$ Memcache-> connect ('memcache host address', 11211 );
You only need to write it once. of course, you can write it in an embedded file.
But you cannot guarantee that it will not be used in functions.
In fact, your core issue was raised from the very beginning: how do you know the changes in content?
If you don't solve this problem, what other technical means are useless?
Hey, you just need to add else {$ row = $ memcache-> get ('results') ;}at the end of the code? What is system configuration? This page is mine. if you do not set the code ($ memcache = new Memcache;
$ Memcache-> connect ('memcache host address ', 11211);) is called in a file, such as IncDB. php. I have to write it again every time. I do not have a dedicated system configuration page.
Since there is no configuration file, put it in IncDB. php.
Connect ('memcache host address', 11211); // The default memcache port number is 201711if ($ row = $ memcache-> get ('result') = false) {// if no results is obtained in the cache, query $ result = mysql_query ("SELECT * FROM userlinks where id = 'a02' and userid = '". $ _ SESSION ['id']. "'Order by datetime desc", $ link); $ row = mysql_fetch_row ($ result); mysql_close ($ link); $ memcache-> set ('results ', $ row); // put the database into the cache after querying} echo "$ row [1]"; echo"
"; Echo" $ row [2] ";?>
To use memcache, you must install the memcached server and the php_memcache extension corresponding to the php version.
For global use
$ Memcache = new Memcache;
$ Memcache-> connect ('memcache host address', 11211 );
You only need to write it once. of course, you can write it in an embedded file.
But you cannot guarantee that it will not be used in functions.
In fact, your core issue was raised from the very beginning: how do you know the changes in content?
If you don't solve this problem, what other technical means are useless?
The content is changed only after the user modifies the data on the EDIT page. That is to say, I can write it in the IncDB. php file and call it? Thank you!
Thank you... I will try.
The memcached content is stored in the memory. Therefore, after the system is restarted, the cached content disappears.
Therefore, it is not enough to write the cache only during modification.
Is there a way? My original intention is to increase the webpage response speed. can memcached be implemented?
In addition, if I have 10 thousand users, I have to store 10 thousand sets of data on the server? I wonder if I understand it correctly. I think it is best to have a client, but I don't know how to store a piece of data on the client and read it.
To cache data, you must consider updating data. for example, the user you mentioned may modify data. You can simply delete the corresponding cache after the user modifies the data in the program.
Thank you for your prompt.
I collapsed, but I couldn't, and no error was reported. That's what I wrote.
Include ("IncDB. php ");
Session_start ();
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211) or die ("link failed! ");
If ($ row = $ memcache-> get ('results') = false ){
$ Result = mysql_query ("SELECT * FROM userlinks where id = 'a02' and userid = '". $ _ SESSION ['id']. "'Order by datetime desc", $ link );
$ Row = mysql_fetch_row ($ result );
Echo "$ row [1]";
Echo"
";
Echo "$ row [2]";
$ Memcache-> set ('results', $ row );
Mysql_close ($ link );}
Else {$ row = $ memcache-> get ('results ');
Echo "$ row [1]";
Echo"
";
Echo "$ row [2]";}
?>
Thank you for your prompt.
I collapsed, but I couldn't, and no error was reported. That's what I wrote.
Include ("IncDB. php ");
Session_start ();
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211) or die ("link failed! ");
If ($ row = $ memcache-> get ('results') = false ){
$ Result = mysql_query ("SELECT * FROM userlinks where id = 'a02' and userid = '". $ _ SESSION ['id']. "'Order by datetime desc", $ link );
$ Row = mysql_fetch_row ($ result );
Echo "$ row [1]";
Echo"
";
Echo "$ row [2]";
$ Memcache-> set ('results', $ row );
Mysql_close ($ link );}
Else {$ row = $ memcache-> get ('results ');
Echo "$ row [1]";
Echo"
";
Echo "$ row [2]";}
?>
Print the variables that may cause errors at various positions of the program. troubleshooting is troublesome.
Back so late? Thank you. Weak questions. what should I do. Don't ask me how much water I have ~~~
Back so late? Thank you. Weak questions. what should I do. Don't ask me how much water I have ~~~
Use var_dump () to print... In other words, you can find some information for this memcache cache to learn from others' blogs...
This question will never end up. let's take a look at the key content mentioned in it, then, when you encounter something you don't understand, it will be more efficient to ask questions. Otherwise, you will have to ask these questions next time.
Memcache
File Cache
I did not understand it after studying it for a few days before posting for help. it may be because the foundation is too poor or the IQ is not at home.
Can I print an error if I write it like this?
Include ("IncDB. php ");
Session_start ();
$ Memcache = new Memcache;
$ Memcache-> connect ('localhost', 11211) or die ("link failed! ");
If ($ row = $ memcache-> get ('results') = false ){
$ Result = mysql_query ("SELECT * FROM userlinks where id = 'a02' and userid = '". $ _ SESSION ['id']. "'Order by datetime desc", $ link );
$ Row = mysql_fetch_row ($ result );
Var_dump ($ row)
Echo "$ row [1]";
Echo"
";
Echo "$ row [2]";
$ Memcache-> set ('results', $ row );
Mysql_close ($ link );}
Else {$ row = $ memcache-> get ('results ');
Echo "$ row [1]";
Echo"
";
Echo "$ row [2]";
Var_dump ($ row )}
?>
Since you are debugging the program and printing more points
So should you give the program running result?
Xuzuning is good. I can't upload the page to the space in my current unit. I don't know the debugging result yet. In addition, do I have more printing code?
Also, I think your strategy is indeed a problem, and I also think it would be best to store data on the client. Each of my pages is very simple and small, but the content is extracted from the database for data display, and the content displayed on each user page will change immediately in addition to the changes made by the user, other general changes are not significant (it may take a few months ). Because every time I increase data from the database slowly, I want to cache the data of each user on the client, and then update the cache when it is modified. I don't know whether it can be implemented. In addition, when I tested these pages locally, the upload space was too slow to be used.
Back to PhpNewnew, I actually learned about caching for more than a week, but I did not understand it. I also know that it is not good for others to be in trouble. Don't worry, if I still haven't learned it, I will fix it in time. Thank you for your attention.
.
You cannot use memcache. This cache technology cannot be used.
It is best to cache the file to the server and specify the time. Then, each time the user changes, the time is recorded. If the time is incorrect, the cache will be re-cached.
This is a concept ..
In fact, your current problem is slow opening, which does not need to be cached at the moment. you can add an index to the data table and it will soon be opened.
I see this post http://www.2cto.com/kf/201407/319353.html
As mentioned above, you can read data from the client storage. I don't know if it works.
You should first clarify the concept! Local is a relative concept:
When the browser accesses your php website, the machine where the browser is located is local.
Php accesses the database server, so the machine on which php is located is local.
The connection you gave didn't mean to cache the data to the client (of course, php is also a client relative to the database)
If you want to cache the page to the client's browser buffer, you only need to set a 304 header when the customer accesses the page again.
Of course, you still need to know whether the data has changed. So the problem returned to the origin:How do you know the content changes?
Only when this problem is solved can there be technical means.
After the user modifies the content, the content changes. I know you have a feeling of playing the piano, but I am really low, and there is no way. Sorry
From another perspective, my original requirement is to improve the response speed without having to access the database every time. What are your suggestions? Thank you.
1. first write a simple memcache instance. There are a lot of online queries. after you are familiar with it, you can apply it to your own code.
2. One point is that memcache has an expiration time. if you set an expiration time, you don't have to worry about the data being stored in it.
3. general ideas,
A. Check memcached every time the data is updated. if memcached exists, update memcached. if memcached does not exist, no action is taken.
B. check memcahced for each data access. if the data exists, retrieve the data and use it directly. if the data does not exist, obtain the data from the database.
4. memcached has a small disadvantage. it has a single data type and does not write data to the hard disk. If you have such requirements, we recommend that you check redis. Redis also supports master-slave backup.
For details about redis usage, refer to 1-3 to change memcached to redis.
I still did not learn. I gave up! Thank you ....