Coder Social home page Coder Social logo

mysql2redis's Introduction

mysql2redis

Redis udf to sync newly modified data from mysql to redis cache.

When you use redis to cache data only(not persistent), you will face the issue that how to sync the newly modified data from mysql to the redis cache? This project aims to provide a high performance and stable redis udf to solve this issue.

Dependencies

please download the dependencies below and compile/install it properly :

Compile

run make from the src dir directly.

make

Install redis udf

please make sure that the lib_mysqludf_redis_v2.so has been put into the mysql plugin dir. By the way, you can examine where is the mysql plugin dir by run ''' mysql_config --plugindir '''. and then connect to your mysql server, run the following command to install the the redis udf.

DROP FUNCTION IF EXISTS redis_servers_set_v2;
DROP FUNCTION IF EXISTS redis_command_v2;
DROP FUNCTION IF EXISTS free_resources;

CREATE FUNCTION redis_servers_set_v2 RETURNS int SONAME "lib_mysqludf_redis_v2.so";
CREATE FUNCTION redis_command_v2 RETURNS int SONAME "lib_mysqludf_redis_v2.so";
CREATE FUNCTION free_resources RETURNS int SONAME "lib_mysqludf_redis_v2.so";

Test redis udf

connect to your mysql server, run the following command to test the the redis udf.

select redis_command_v2("lpush","crmInboxEvents11",json_object(json_members("op","insert","value","valuettt")));

select redis_servers_set_v2("192.168.0.118",6379);
select redis_command_v2("lpush","crmInboxEvents11",json_object(json_members("op","insert","value","valuettt")));

select redis_command_v2("hset","hkey","hfield",json_object(json_members("op","insert","value","valuettt")));


select free_resources();
select redis_servers_set_v2("192.168.0.118",6379);

What's more

you should create a trigger which will lpush the newly modified data to redis list juste as the following example:

DELIMITER $$
CREATE TRIGGER insert_trigger AFTER INSERT ON email_folder
FOR EACH ROW BEGIN
 SET @ret=
  	 redis_command_v2("lpush","crmInboxEvents",
 						  json_object
                           (
 						    	json_members
 								(
 										"op",
 										"insert",
 										"value",
 										json_object
 										(
 											NEW.Id as "id",NEW.type as "type",
 											NEW.mailserver_id as "mailserverId",NEW.sender as "sender",
 											NEW.sender_name as "senderName",NEW.recevier as "recevier",
 											NEW.replyto as "replyto",NEW.bbemails as "bbemails",
 											NEW.ccemails as "ccemails",NEW.subject as "subject"
                                         )
 									)
 					    	)
 				      );
END$$
DELIMITER ;

Finally

Now all update events for that mysql table has been transfer to a redis queue. You need to start a thread(or process) to handle these update events and update the redis cache properly.

For example, you can start a java process to poll the redis queue , and pop the events, then handle it and update the redis cache properly.

Performance test

In my poor labtop, 10,000 row data were synced into redis in 0.4 second.

======== Please feel free to contact me([email protected]) if you have any questions.

mysql2redis's People

Contributors

aborn avatar cui422 avatar dawnbreaks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mysql2redis's Issues

LTRIM command not support?

i use this code, but list is not trimed:

SET v_redis_ret=redis_command_v2('LTRIM', 'UserReadingRecord4', 0, 4);

lpush twice

DELIMITER $$
CREATE TRIGGER user_consult_insert_trigger AFTER INSERT ON user_consult
FOR EACH ROW BEGIN
set @id = redis_servers_set_v2("127.0.0.1", 6369);
set @ret = redis_command_v2("", 0, "lpush",
concat("user-consultation-list-", NEW.user_id),
concat(NEW.id)
);
set @ret = redis_command_v2("", 0, "lpush",
concat("consultant-consultation-list-", NEW.consultant_id),
concat(NEW.id)
);
set @ret = redis_command_v2("", 0, "set",
concat("user_consult-", NEW.id),
json_object(
NEW.id as "id",
NEW.user_id as "user_id",
NEW.consultant_id as "consultant_id",
NEW.service_id as "service_id",
NEW.service_type as "service_type",
NEW.title as "title",
NEW.content as "content",
NEW.ctime as "ctime",
NEW.reply_num as "reply_num"
)
);
END$$
DELIMITER ;

the code above is my trigger.this trigger always insert primary id into user-consultation-list* twice

SOMETHING WRONG WITH THE MAKE COMMAND

base on MySQL-server-5.6.32-1.linux_glibc2.5.i386.rpm , when I make the mysql2redis,the below message showed:

[root@localhost mysql2redis]# make
gcc -Werror -O2 -g /usr/bin/mysql_config --include -I/usr/local/include -I/usr/local/apr/include -I./ -I. -fPIC -shared -rdynamic lib_mysqludf_redis.c -lhiredis -L"/usr/lib64/mysql/plugin" -L/usr/local/apr/lib -lapr-1 -laprutil-1 -o lib_mysqludf_redis_v2.so
cc1: warnings being treated as errors
In file included from lib_mysqludf_redis.h:26,
from lib_mysqludf_redis.c:5:
/usr/include/mysql/m_string.h: In function ‘skip_trailing_space’:
/usr/include/mysql/m_string.h:261: error: cast from pointer to integer of different size
/usr/include/mysql/m_string.h:261: error: cast to pointer from integer of different size
/usr/include/mysql/m_string.h:263: error: cast from pointer to integer of different size
/usr/include/mysql/m_string.h:263: error: cast to pointer from integer of different size
/usr/include/mysql/m_string.h:265: error: cast from pointer to integer of different size
make: *** [compile] Error 1

the function ‘skip_trailing_space’ like below:

static inline const uchar *skip_trailing_space(const uchar *ptr,size_t len)
{
const uchar *end= ptr + len;

if (len > 20)
{
const uchar *end_words= (const uchar *)(intptr)
(((ulonglong)(intptr)end) / SIZEOF_INT * SIZEOF_INT);
const uchar *start_words= (const uchar *)(intptr)
((((ulonglong)(intptr)ptr) + SIZEOF_INT - 1) / SIZEOF_INT * SIZEOF_INT);

DBUG_ASSERT(((ulonglong)(intptr)ptr) >= SIZEOF_INT);
if (end_words > ptr)
{
  while (end > end_words && end[-1] == 0x20)
    end--;
  if (end[-1] == 0x20 && start_words < end_words)
    while (end > start_words && ((unsigned *)end)[-1] == SPACE_INT)
      end -= SIZEOF_INT;
}

}
while (end > ptr && end[-1] == 0x20)
end--;
return (end);
}

THANKS FOR HELP.

when i use redis_command_v2 in cursor, the data count is wrong

BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE v_id INT;
DECLARE v_ret INT;
DECLARE cur CURSOR FOR SELECT u.user_id FROM kid_users u WHERE u.latest_login_time>=DATE(DATE_ADD(NOW(),INTERVAL -1 DAY)) AND EXISTS(SELECT * FROM kid_child_info WHERE user_id=u.user_id AND gender IN(0,1)) ORDER BY u.latest_login_time DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur;
read_loop: LOOP
	FETCH cur INTO v_id;
	IF done THEN
		LEAVE read_loop;
	END IF;

	SET v_ret=redis_command_v2('LPUSH', 'calcReadingRecommendForYou', CONCAT(v_id));
END LOOP;
CLOSE cur;

END

undefined symbol: info

os : centos-5.6
jemalloc: jemalloc-3.5.1
apr: apr-1.4.6
apr-util: apr-util-1.5.2
hiredis: hiredis-master
#1126 - Can't open shared library 'lib_mysqludf_redis_v2.so' (errno: 0 /opt/mysql-5.5.15/lib/plugin/lib_mysqludf_redis_v2.so: undefined symbol: info)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.