Improve PHP performance by caching Database results

Source: Internet
Author: User

As we all know, the results of caching database queries can significantly shorten script execution time and minimize the load on the database server. If the data to be processed is basically static, the technique will be very effective. This is because many data requests to the remote database can eventually be satisfied from the local cache, eliminating the need to connect to the database, execute the query, and get the results.

However, when you use a database that is on a different computer than the WEB server, caching a database result set is usually a good idea. However, determining the best caching strategy based on your situation is a challenge. For example, for applications that use the most recent database result set, the time-triggered caching method (the common method of caching systems, which assumes that the cache is regenerated each time the expiration timestamp is reached) may not be a satisfactory solution. In this case, you need to adopt a mechanism that notifies the application whenever the application needs to change the cached database data so that the application keeps cached out-of-date data in line with the database. In this case, it is convenient to use the database change notification.

Getting Started with "Database change notifications"

The usage of the database change notification attribute is very simple: Create a notification handler for the notification execution-a Pl/sql stored procedure or client OCI callback function. Then, register a query for the database object to which you want to receive change notifications, so that the notification handler is invoked whenever the transaction changes any of its objects and commits. In general, the notification handler sends the name of the modified table, the type of the change, and the row ID (optional) of the changed line to the client listener, so that the client application can perform the appropriate processing in the response.

To see how the Database Change notification feature works, consider the following example. Suppose your PHP application accesses the OE. Orders stored in the Orders table as well as OE. The order items stored in the Order_items. Since it is rare to change the information that has been placed on orders, you may want the application to simultaneously cache query result sets for orders and Order_items tables. To avoid accessing expired data, you can use database change notification, which makes it easy for your application to learn about changes to the data stored in the two tables above.

You must first grant the change NOTIFICATION system permissions and EXECUTE on Dbms_changenotification permissions to the OE user to register queries against ORDERS and Order_items tables in order to receive notifications and responses to this A DML or DDL change made by two tables. To do this, you can execute the following commands from the SQL command-line tool, such as Sql*plus.

CONNECT / AS SYSDBA;
GRANT CHANGE NOTIFICATION TO oe;
GRANT EXECUTE ON DBMS_CHANGE_NOTIFICATION TO oe;

Make sure that the Init.ora parameter job_queue_processes is set to a value other than 0 to receive pl/sql notifications. Alternatively, you can use the following ALTER SYSTEM command:

ALTER SYSTEM SET "job_queue_processes" = 2;

Then, after you connect with Oe/oe, you can create a notification handler. But first, you must create a database object that will be used by the notification handler. For example, you might want to create one or more database tables to notify the handler to log changes to the registry. In the following example, you will create a nfresults table that records the date and time the change occurred, the name of the table that was modified, and a message stating whether the notification handler successfully sent the notification message to the client.

CONNECT oe/oe;
CREATE TABLE nfresults (
operdate DATE,
tblname VARCHAR2(60),
rslt_msg VARCHAR2(100)
);

In practice, you may need to create more tables to record information such as notification events and row IDs for the rows that are changed, but for this article, the Nfresults table is perfectly up to suit.

Using Utl_http to send notifications to clients

You may also want to create one or more pl/sql stored procedures and call these stored procedures from the notification handlers to achieve a more maintainable and flexible solution. For example, you might want to create a stored procedure that sends notification messages to clients. "Listing 1" is the Pl/sql process sendnotification. This procedure uses the UTL_HTTPPL package to send a change notification to the client application.

Listing 1. Using Utl_http to send notifications to clients

CREATE OR REPLACE PROCEDURE sendNotification(url IN VARCHAR2,
tblname IN VARCHAR2, order_id IN VARCHAR2) IS
req  UTL_HTTP.REQ;
resp UTL_HTTP.RESP;
err_msg VARCHAR2(100);
tbl VARCHAR(60);
BEGIN
tbl:=SUBSTR(tblname, INSTR(tblname, '.', 1, 1)+1, 60);
BEGIN
req := UTL_HTTP.BEGIN_REQUEST(url||order_id||'&'||'table='||tbl);
resp := UTL_HTTP.GET_RESPONSE(req);
INSERT INTO nfresults VALUES(SYSDATE, tblname, resp.reason_phrase);
UTL_HTTP.END_RESPONSE(resp);
EXCEPTION WHEN OTHERS THEN
err_msg := SUBSTR(SQLERRM, 1, 100);
INSERT INTO nfresults VALUES(SYSDATE, tblname, err_msg);
END;
COMMIT;
END;
/

As shown in Listing 1, sendnotification to Utl_http. Sends a notification message to the client in the form of an HTTP request issued by the Begin_request function. This URL contains the order_id that has changed the row in the ORDERS table. Then, it uses utl_http. Get_response gets the response information sent by the client. In fact, SendNotification does not need to handle the entire response returned by the client, but instead gets only a short message stored in the Reason_phrase field of the RESP record (describes the status code).

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.