2

UPDATE: I've decided to take the advice below and implement a Memcached tier in my app. Now I have another thought. Would it be possible/a good idea to do an AJAX request on a poll (say every five or ten minutes) that checks Memcached and then updates when Memcached has expired? This way the latency is never experienced by the end user because it's performed silently in the background.


I'm using Directed Edge's REST API to do recommendations on my web app. The problem I'm encountering is that I query for a significant number of recommendations in multiple places across the site, and the latency is significant, making the page load something like 2-5 seconds for each query. It looks terrible.

I'm not using Directed Edge's PHP bindings, and instead am using some PHP bindings I wrote myself. You can see the bindings on GitHub. I'm connecting to their API using cURL.

How can I cache the data I'm receiving? I'm open to any number of methods so long as they're fairly easy to implement and fairly flexible.

Here's an example of the client code for getting recommendations.

$de = new DirectedEdgeRest();
$item = "user".$uid;
$limit = 100;
$tags = "goal";
$recommendedGoals = $de->getRecommended($item, $tags, $limit);

1 Answer 1

2

You can cache to a file using serialize and file_put_contents:

file_put_contents("my_cache", serialize($myObject));

You could also cache to memcached or a database.

Sign up to request clarification or add additional context in comments.

7 Comments

Well, I'm hoping to add a memcached tier, though haven't yet. Do you mind giving me a high-level overview of exactly how that would work, with some foo bar-like code? Just curious how difficult working with memcached really is.
memcached is dead simple, you just need a server that supports it. You just call memcache_set("key", "value") when you want to store something, and memcache_get("key") when you want to retrieve it.
Wow, that's awesome. Is there any particular key-naming conventions you use yourself? Like a UUID or UID prefix or something?
For objects I usually just use "class_name_database_id". So a photo with ID 4325 would be "photo_4325". For more complex things I would just use a certain name and append the object's ID.
Awesome. I'm going to have to do some deeper digging into this, but I'm off install libmemcached now. This seems at first glance to be a lot easier to implement than I originally thought.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.