Memcached made easy with PHP — What it means?

If you need to learn about memcached, go google. In this post, I will merely tell how to implement it using some simple pieces of code and make your website more efficient. Every good piece of code needs 3 working parts of it:
1. Good Logic
2. Easy to implement and maintain
3. ..tell me when you know..

In this part of What it means, I’ll describe how you can start working with memcached with PHP project in less than 10 minutes(I seriously hope so).
P.S. You do need to have working knowledge of PHP-FPM/Apache services before going ahead.

Let’s start by scraping up what we need to get Memcached up and running in our code:

  1. We need the Memcached service. Download and install from https://memcached.org/downloads, it’s that direct. I just did and
  2. Next we need PHP-Memcached extension so that our code can well locate class.
    In order to so that, just run . Yes, It’s that easy too. (psst... don’t forget to restart or once extension is enabled)
  3. All we need now is to test if it runs. here’s a script to test it:

Now that we’ve tested it. We can do the fun(~boring) part. Set it up to be directly used in our code! Have a look.
Following is a Model class which can be used to directly contact our service and functions. You can add more custom functions and have a look at available functions here.

Tip: The can be any string, it just signifies the name of the connection, so every-time, you make an object of the Class, it ensures the same connection is used and multiple connections are not made.

Above, is a very basic class which can be used for primary purposes of Memcache. That’s IT!

Now you can easily implement it in any controller and use it as follows!!

In another read, I will explain how you can streamline the process of getting and setting your pages, so that the repetitive code inside a method can be declined further.

If you have any questions, just drop a comment. Enjoy!

jazeabby