MySQL to refresh memcached - php

We have a web application running on LAMP stack, the application depends on various services. These services gets data from cache (memcached) which is being refreshed using cron (from MySQL). Cron processes are running every 5 mins.
In this approach we can not serve data which is updated recently as cache is getting refreshed every 5 mins.
Is there any mechanism exists which can trigger cache refresh as soon as data gets updated in MySQL?

I don't know if this is the best solution, but what you can do is create MySQL trigger which gets executed on insert/update/delete.
Inside that MySQL trigger execute a UDF. From that UDF you can execute any PHP script using sys_exec().
Read about Triggers
Read about UDF
Read about using sys_exec() and more

My solution for this problem was to have the MySQL query in a function with the memcached query, for example (in Python, because I don't know PHP, you can look at it an change it to PHP):
def insert(user,value):
#execute on memcached first
key="user:"+user
memcClient.set(key,value)
#then execute it on MySQL
cursor.execute("INSERT INTO tables VALUES (%s,%s)",(user,value))
db.commit()
And you will do the same for delete and update, sorry I couldn't do it in PHP, I'm just a teen coder, good luck.

What about identifying the mysql updation at php level only & refresh the memcache accordingly ?
<?php
if($update_db){ // if we need to update the db then update db & memcacache together !
// Code to Update the database ....
// Code to Reset the memcached keyvalue pair.
}
?>
In the approach suggested by Manu, we are using expensive db trigger which is actually not needed to achieve the cache update.

Related

Does (or, how does) MySQL natively take care of concurrent transactions?

Maybe this is an obvious question, but it's just something I'm unsure of. If I have two standalone PHP applications running on one LAMP server, and the two PHP applications share the same MySQL database, do I need to worry about data integrity during concurrent database transactions, or is this something that MySQL just takes care of "natively"?
What happens if the two PHP applications both try to update the same record at the same time? What happens if they try to update the same table at the same time? What happens if they both try to read data from the database at the same time? Or if one application tries to read a record at the same time as the other application is updating that record?
What happens if the two PHP applications both try to update the same record at the same time?
What happens if they try to update the same table at the same time?
What happens if they both try to read data from the database at the same time?
Or if one application tries to read a record at the same time as the other application is updating that record?
This depend from several factor ..
the db engine you are using
the locking policy / transaction you have setted for you envirement .. or for you query
https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html
https://dev.mysql.com/doc/refman/8.0/en/innodb-locks-set.html
the code you are using .. you could use a select for update for lock only the rows you want modify
https://dev.mysql.com/doc/refman/8.0/en/update.html
and how you manage transaction
https://dev.mysql.com/doc/refman/8.0/en/commit.html
this is just a brief suggestion

trigger SOLR update after MySQL update

I'm planning to use SOLR for full-text search. Question: when I update my MySQL-database, is there any way to trigger a SOLR-update after I updated my database, so the data will be exactly the same in both MySQL and SOLR? DataImportHandler? Or UpdateHandler? I'm using PHP, and sometimes about 100 insert/update/delete-queries/minute (not always).
Either you call Solr to update data from PHP when you are updating DB or you can set up the cron job do do so.
They are planning to add scheduler do Solr: http://wiki.apache.org/solr/DataImportHandler#Scheduling
Run a cron job in the background to perform a delta index of the database.

Run script when someone inserts or updates a row in certain Table

I am using MYSQL under ubuntu. I want to run a certain program automatically when inserting or updating row in a certain table. The program is actually sending request to a php on the same server. The php script is implemented and it notifies all clients that "data is updated, please get it". How can I do it?
Thank You
best solution is create cron job and use system command in cron file.
If you have control of the script, the easiest way would be to create your own query() method that wraps around whatever SQL query call you need to make. You can put something in there to see if there's an UPDATE/DELETE/INSERT and if so fire off your "data updated" notice.
Probably cannot be done without major security issues on mysql server.
you could to this from php. either execute the program when you send the query to mysql or create a cronjob

call php script from oracle trigger

Question is following: I have an oracle trigger after row insert. From trigger, I want to call a php script and pass just inserted row fields as parameters.
Insert is coming from very old legacy application with a very little chance of looking at the source code. Rows are inserted frequently, could be batches of ~1000 rows at a time or could be one row in 30minutes, so checking this table every, let's say, 5 seconds is not an option.
So, Idea is to have oracle trigger, which would be triggered every time on insert and call my php script? Any ideas?
Thanks in advance...
When you say "php script" do you literally mean a command line script, or a chunk of php being run through apache/etc.
If its the former, then go with OMG Ponies. Otherwise I'd use UTL_HTTP to make the call to Apache/PHP. Actually, I'd probably consider going with this anyway ( updating your php/c# if needed ).
Just remember though.. Triggers are transactional... if you absolutely MUST call out from a trigger be aware that your trigger may run several times ( due to query restart ) and may rollback completely, resulting in your external (presumably non-transactional) php call now being invalid. If your php can't handle this then perhaps have your trigger creating a job or even a message into an AQ or something, this would also speed up handling, you probably don't really want your insert to be waiting on an external web call.
I want to call a php script when some row is inserted or updated in some table via oracle triggers . Currently I m using this but its not much of help.
CREATE OR REPLACE TRIGGER test_script
BEFORE INSERT OR UPDATE OR DELETE ON STATES
FOR EACH ROW
BEGIN
-- Flags are booleans and can be used in any branching construct.
CASE
WHEN INSERTING THEN
:'/! echo C:/wamp/bin/php/php5.3.5/php.exe C:/wamp/www/csv.php >> C:/wamp/www/log.txt'
WHEN UPDATING THEN
-- Include any code specific for when the trigger is fired from an UPDATE.
WHEN DELETING THEN
-- Include any code specific for when the trigger is fired from an DELETE.
END CASE;
END;
/

Live update notification on database changes MYSQL PHP

I was wondering how to trigger a notification if a new record is inserted into a database, using PHP and MySQL.
You can create a trigger than runs when an update happens. It's possible to run/notify an external process using a UDF (user defined function). There aren't any builtin methods of doing so, so it's a case of loading a UDF plugin that'll do it for you.
Google for 'mysql udf sys_exec' or 'mysql udf ipc'.
The simplest thing is probably to poll the DB every few seconds and see if new records have been inserted. Due to query caching in the DB this shouldn't effect DB performance substantially.
MySQL does now have triggers and stored procedures, but I don't believe they have any way of notifying an external process, so as far as I know it's not possible. You'd have to poll the database every second or so to look for new records.
Even if it were, this assumes that your PHP process is long-lived, such that it can afford to hang around for a record to appear. Given that most PHP is used for web sites where the code runs and then exits as quickly as possible it's unclear whether that's compatible with what you have.
If all your database changes are made by PHP I would create a wrapper function for mysql_query and if the query type was INSERT, REPLACE, UPDATE or DELETE I would call a function to send the respective email.
EDIT: I forgot to mention but you could also do something like the following:
if (mysql_affected_rows($this->connection) > 0)
{
// mail(...)
}
One day I ask in MySQL forum if event like in Firebird or Interbase exist in MySQL and I see that someone answer Yes (I'm really not sure)
check this : http://forums.mysql.com/read.php?84,3629,175177#msg-175177
This can be done relatively easily using stored procedures and triggers. I have created a 'Live View' screen which has a scrolling display which is updated with new events from my events table. It can be a bit fiddly but once its running its quick.

Categories