Mysql-based memcache-like Cache

Source: Internet
Author: User
When I was in sae, the built-in mysql class was quite comfortable. Later, I encapsulated the SQL statement executed by the runSql method. The getData method obtained the result array and the Transactions method implemented the transaction. MysqlnewFaMysql ();} functionset ($ key, $ value, $ expire31536000) {the default validity period is one year $ nowti.

When I was in sae, the built-in mysql class was quite comfortable. Later, I encapsulated the SQL statement executed by the runSql method. The getData method obtained the result array and the Transactions method implemented the transaction. Mysql = new FaMysql ();} function set ($ key, $ value, $ expire = 31536000) {// The default validity period is one year. $ now = ti

When I was in sae, the built-in mysql class was quite comfortable. Later, I encapsulated the SQL statement executed by the runSql method. The getData method obtained the result array and the Transactions method implemented the transaction. Mysql = mysql_connect ($ options ['host'], $ options ['username'], $ options ['Password']) or die ("Database connection error. "); mysql_select_db ($ options ['database'], $ this-> mysql); mysql_query (" set names '". $ options ['charset']. "'");} public function Transactions (array $ SQL) {mysql_query ("BEGIN"); $ r = 1; foreach ($ SQL as $ key => $ row) {$ res = mysql_query ($ row); if (! $ Res) {$ r = 0 ;}} if (! $ R) {mysql_query ("ROLLBACK");} else {mysql_query ("COMMIT");} mysql_query ("END");} public function runSql ($ SQL) {mysql_query ($ SQL) or die (mysql_error ();} public function getData ($ SQL) {$ result = mysql_query ($ SQL) or die (mysql_error ()); $ array = array (); while ($ row = mysql_fetch_assoc ($ result) {$ array [] = $ row;} return $ array ;}} didn't buy vps at the beginning, the RP host does not provide memcache cache, so you can implement it yourself. The set Method stores data, the get method obtains data, and the del method deletes data. Only string is supported. Class FaMemcache {private $ mysql; private $ TABLENAME = 'memcache'; public function _ construct () {$ this-> mysql = new FaMysql ();} function set ($ key, $ value, $ expire = 31536000) {// The default validity period is one year $ now = time (); $ valid = $ now + $ expire; $ data = $ this-> mysql-> getData ("SELECT Fa_key FROM '$ this-> TABLENAME 'where Fa_key =' $ key '"); if (count ($ data )! = 0) {$ this-> mysql-> runsql ("updat' $ this-> TABLENAME 'set Fa_value = '$ value ', valid = '$ valid' WHERE Fa_key =' $ key '");} else {$ this-> mysql-> runsql ("insert into '$ this-> TABLENAME' (Fa_key, Fa_value, Valid) VALUES ('$ key',' $ value ', '$ valid') ") ;}} function get ($ key) {$ now = time (); $ data = $ this-> mysql-> getData ("SELECT Fa_value FROM '$ this-> TABLENAME 'where Fa_key =' $ key' AND $ now> Valid "); if (count ($ Data )! = 0) {return $ data [0] ['fa _ value'];} else {return null;} function del ($ key) {$ this-> mysql-> runsql ("delete from '$ this-> TABLENAME 'where Fa_key =' $ key '");}}

Original article address: mysql-based imitation memcache cache class. Thank you for sharing it with the original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.