We have successfully installed and stated Memcached. Now we will simply connect to the Memcached and work simple operations.
Connect To The Memcached with Telnet
The simples tool to connect Memcached and make operations is telnet. Memcached default tcp port is 11211 so to connect memcached we will issue following commands.
$ telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'.
Set Simple Value to Memcached with Telnet
We will set simple value to the memcached to test it.
$ telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. set poftut 0 900 9
Ok there is no problem it seems. Using telnet is the simplest way but not a productive and practical way. So to store values into the Memcached we will write some applications in different languages. We prefer here python because its simplicity and popularity.
Install Python3 for Memcached
We will install python3 libraries for Memcached. There are alternatives for libraries by the way.
$ dnf install python3-memcached.noarch -y
Write Simple Python App for Memcached
Open python3 interactive shell for issuing commands.
$ python3 Python 3.5.1 (default, Sep 19 2016, 10:16:17) [GCC 6.1.1 20160621 (Red Hat 6.1.1-3)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Import Memcached library to the shell.
>>> import memcache
Create a connection to the Memcached services with default settings.
>>> client=memcache.Client([('127.0.0.1',11211)])
Create some data as python dictionary object.
>>> samp={"name":"poftut.com"}
Add the data to the Memcached with “sample” tag and for 15 minutest. After 15 minutes our object will be automatically deleted.
>>> client.set("sample",samp,time=15) True
All of the code
>>> import memcache >>> client=memcache.Client([('127.0.0.1',11211)]) >>> samp={"name":"poftut.com"} >>> client.set("sample",samp,time=15) True