Patrick Galbraith ([info]capttofu) wrote,
@ 2007-11-30 10:37:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:memcached, mysql

Memcached MySQL Functions Released!
The first initial Memcached MySQL UDF functions version has been released. You can download them at:

http://download.tangent.org/memcached_functions_mysql-0.1.tar.gz

Developed by Brian Aker and Patrick Galbraith, These are a number of MySQL user defined functions based on libmemcached (http://tangent.org/552/libmemcached.html) mirroring libmemcached client functions.

Included are:

memc_servers_set() - sets list of memcached servers to use

memc_set(hash, value) - sets a value keyed by hash in memcached
memc_get(hash) - retrieves a value from memcached keyed by hash
memc_delete() - deletes a value from memcached keyed by hash
memc_append() - appends to the end of a value in memcached keyed by hash
memc_prepend() - prepends to the beginning of a value in memcached keyed by hash
memc_increment() - increments numerical values stored in memcached (think sequence) keyed by hash
memc_decrement() - decrements numerical values stored in memcached keyed by hash
memc_replace() - replaces values with new values keyed by hash

What do these functions give you? The ability to either write to or read from memcached on read or write queries, within mysql. This means you could do something like this:

CREATE TABLE `jsoncache` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`memcache_key` varchar(32) DEFAULT NULL,
`json` mediumtext,
PRIMARY KEY (`id`)
)

mysql> delimiter | mysql> CREATE TRIGGER `jsoncache_trigger` BEFORE INSERT ON `jsoncache` FOR EACH ROW begin set @id= concat('jsoncache:', NEW.id); set @tt= memc_set(@id, NEW.json); set NEW.memcache_key = @id; end |
Query OK, 0 rows affected (0.05 sec)

mysql>delimiter ;

mysql> insert into jsoncache (json) values ('{{json stuff}}');
Query OK, 1 row affected (0.00 sec)

mysql> select * from jsoncache;

+----+--------------+----------------+
| id | memcache_key | json |
+----+--------------+----------------+
| 1 | jsoncache:1 | {{json stuff}} |
+----+--------------+----------------+
1 row in set (0.00 sec)

mysql> select memc_get('jsoncache:1');

+-------------------------+
| memc_get('jsoncache:1') |
+-------------------------+
| {{json stuff}} |
+-------------------------+
1 row in set (0.00 sec)

Wow! This is great, because now when you insert (write) to MySQL (in my case it would be to a master), you have that data automagically written to memcached!

You can either have a write-through or read-through way of writing data to memcached. I'm sure others will come up with other uses.



(Post a new comment)


[info]awfief
2007-11-30 05:02 pm UTC (link)
Is this a way to get memcached without a lot of programming? Or rather, by changing the queries instead of the code? ie, program-language independence & using memcached?

(Reply to this)(Thread)


[info]krow
2007-11-30 06:27 pm UTC (link)
Bingo :)

Less code is needed in a couple of ways:

1) You can blow out memcached when objects are stale.
2) You can push simple bits of data into memcached when needed.

Number 2 is going to get extended :)

(Reply to this)(Parent)

atomicity, less code
[info]capttofu
2007-11-30 06:58 pm UTC (link)
Yes, and also yes.

Also, this makes it atomic. You insert into db, have a trigger as I showed, memcached gets the data upon insert into mysql.

You can also have the read-through be in one shot. You do something like

'select col, memcached_set('namespace:key', col) from foo';

(my syntax might be off) The idea being you select get data and cache data at the same time!

(Reply to this)(Parent)

Idetrorce
(Anonymous)
2007-12-15 02:08 pm UTC (link)
very interesting, but I don't agree with you
Idetrorce

(Reply to this)(Thread)

Re: Idetrorce
(Anonymous)
2007-12-20 08:42 pm UTC (link)
It'd be nice if you explain what you don't "agree" with.

(Reply to this)(Parent)(Thread)

Re: Idetrorce
(Anonymous)
2007-12-22 07:09 am UTC (link)
HI Patrick,

This is Murali...Just gone through the article and downloaded the UDF functions.

But can u tell me how i can install this to my MYSQL server running in LINUX.

(Reply to this)(Parent)

Install UDF functions to MYSQL SERVER
(Anonymous)
2007-12-22 07:12 am UTC (link)
Hi Patrick Galbraith,

Sorry i mistakenly have posted my comment in response to that user.

Can u please tell me how can i install those UDF functions to my MYSQL server which runs on LINUX.?

(Reply to this)

My blog about health and wealth
(Anonymous)
2008-01-20 04:33 pm UTC (link)
Why you should buy viagr lawsuit?
First of all, if you buy us gneric viagra you get quality service and great discounts!
Why would you need pfizer viara?
Viagra in the first place make a uverenost, most of the problems are solved in the privacy of using our products. We will help you learn infinite possibilities in the intimate lives.
Why do you have to use our shop?
You pay only real value of the goods, without deductions resellers percent! Ordering pfier viagra us, you get free delivery and discounts on other products.
On our site you can order levtra. The lowest price. Large discounts. Full anonymity. Free shipping.
More info about viagra onlie you can be found on our website http://buy-viagra-online-pharmacy-in-canada.info/buy-cheap-viagra-online-now/bestellen-online-viagra.php

(Reply to this)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…