I’ve been using Basho Riak for a few weeks now. I’ve only had to store string data.
However, I’m looking at using it to store Images and I would like some idea how I can do this with the PHP Client.
Below is the basic code to store data:
require_once('riak-php-client/riak.php');
# Connect to Riak
$client = new RiakClient('127.0.0.1', 8098);
# Choose a bucket name
$bucket = $client->bucket('test');
# Supply a key under which to store your data
$person = $bucket->newObject('string_key', 'string_data');
# Save the object to Riak
$person->store();
Do I just base64_encode the image and then store the resulting string?! Or is there a better way?!
Thanks in advance.
You’ll want to use
RiakBucket::newBinary()andRiakBucket::getBinary()if you want to store unencoded binary data into Riak with the PHP client.Produces output:
In my performance tests, both approaches have basically the same overhead from Riak’s perspective. Spending cycles on base64 encoding / decoding (plus under the hood, the base64 data is then json encoded/decoded) puts the binary approach ahead overall.
Edit: Also note that there’s a ~50mb upper limit for data stored in a Riak binary object (see this post) due to a limitation in the Erlang backend. Realistically if you’re getting anywhere near that, you might want to rethink how you’re storing those images, that’s a lot of data to send on the pipe if you’re accessing those frequently, something like NFS or another local filesystem cache is probably a better idea.