标签:info name exists 完成 res 语言 markdown 相同 操作
读取步骤:
127.0.0.1:6379> hgetall w3ckey
(empty list or set) #最开始在reids中没有w3ckey的K-V对。
127.0.0.1:6379>
drop database if exists mysqlRedis;
create database mysqlRedis;
use mysqlRedis;
create table test1(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(64),
age INT,
description VARCHAR(1000),
primary key(id));
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
#include <string.h>
#include <hiredis/hiredis.h>
int gxupdate(UDF_INIT * initid, UDF_ARGS * args, char * is_null, char * error) {
redisContext * c = redisConnect("127.0.0.1", 6379);
if(c->err) {
redisFree(c);
return 1;
}
/*
//如果设有密码为ubuntu
redisReply *reply;
char strReply[] = "AUTH ubuntu";
reply = (redisReply*)redisCommand(c, strReply);
freeReplyObject(reply);
reply = NULL;
*/
const char * command1 = "HMSET w3ckey id %d name %s age %d description %s";
redisReply * r = (redisReply *)redisCommand(c, command1,
*(int*)args->args[0], args->args[1], *(int *)args->args[2], args->args[3]);
if (r == NULL) {
return 1;
}
if (!((r->type == REDIS_REPLY_STATUS) && (strcasecmp(r->str, "OK") == 0))) {
freeReplyObject(r);
redisFree(c);
return 1;
}
freeReplyObject(r);
return 0;
}
my_bool gxupdate_init(UDF_INIT * initid, UDF_ARGS * args, char * message) {
return 0;
}
编译为动态库:
gcc -shared -fPIC -I /usr/include/mysql -o udfredis.so mysqlUDFdemo.c /usr/local/lib/libhiredis.a
编译完成之后拷贝动态库udfgx.so到 /usr/lib/mysql/plugin/文件夹中,并修改成用户对应权限。
use mysqlRedis;
drop function if exists gxupdate;
create function gxupdate returns INTEGER soname "udfredis.so";
drop trigger if exists insert_redis;
drop trigger if exists update_redis;
drop trigger if exists delete_redis;
delimiter |
create trigger insert_redis
after insert on test1
for each row
begin
declare ret int;
select gxupdate(NEW.id, NEW.name, NEW.age, NEW.description) into @ret;
#必须加into @ret,否则返回错误ERROR 1415 (0A000)
#at line 6: Not allowed to return a result set from a trigger
#insert只有NEW变量。
#update有NEW和OLD变量。
#delete只有OLD变量。
end|
create trigger update_redis
after update on test1
for each row
begin
declare ret int;
select gxupdate(NEW.id, NEW.name, NEW.age, NEW.description) into @ret;
end|
create trigger delete_redis
after delete on test1
for each row
begin
declare ret int;
select gxupdate(OLD.id, OLD.name, OLD.age, OLD.description) into @ret;
end|
delimiter ;
注意,在MySQL中创建UDF的时候,insert, update和delete不能写成一个触发器,只能分别定义成三个触发器。
[root@VM_24_16_centos mysql_redis]# redis-cli
127.0.0.1:6379> hgetall w3ckey
(empty list or set)
127.0.0.1:6379>
redis中无key w3ckey 对应的value。
mysql> insert into test1 (name, age, description) values ("ggglwlop", 23, "ddddgdg");
Query OK, 1 row affected (0.02 sec)
mysql>
插入mysql。
127.0.0.1:6379> hgetall w3ckey
1) "name"
2) "ggglwlop"
3) "description"
4) "ddddgdg"
5) "likes"
6) "27"
7) "visitors"
8) "23"
127.0.0.1:6379>
MySQL中有了对应的数据,说明mysql通过triger+udf的方式把改动更新到了redis中。
http://blog.csdn.net/socho/article/details/52292064
https://www.cnblogs.com/linuxbug/p/4950626.html
https://www.cnblogs.com/tommy-huang/p/4703514.html 使用redis作为mysql缓存时的redis结构设计。
http://blog.csdn.net/shikaiwencn/article/details/51792059 需要根据实际需求来灵活设计redis kv关系。
https://www.cnblogs.com/bruceleeliya/archive/2009/05/23/Linux-C-Mysql.html 使用mysql的C API访问mysql。
https://www.2cto.com/database/201110/108925.html #mysql udf。
https://www.cnblogs.com/linuxbug/p/4950626.html #udf使用的一个例子。
https://www.jianshu.com/p/4381a38403a1
http://blog.csdn.net/socho/article/details/52292064
标签:info name exists 完成 res 语言 markdown 相同 操作
原文地址:https://www.cnblogs.com/yaoyangding/p/12109204.html