Memcached与Telnet和Python简介

我们已成功安装并声明Memcached。现在我们只需连接到Memcached 简单的操作。

null

用Telnet连接Memcached

连接Memcached并进行操作的simples工具是telnet。Memcached的默认tcp端口是11211,因此要连接Memcached,我们将发出以下命令。

$ telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'.

使用Telnet将Simple Value设置为Memcached

我们将将简单值设置为memcached以测试它。

$ 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

好吧,看来没问题。使用telnet是最简单的方法,但不是一种有效和实用的方法。为了将值存储到Memcached中,我们将用不同的语言编写一些应用程序。我们更喜欢python,因为它的简单性和流行性。

为Memcached安装Python3

我们将为Memcached安装python3库。顺便说一句,图书馆还有其他选择。

$ dnf install python3-memcached.noarch -y

为Memcached编写简单的Python应用程序

打开python3交互式shell以发出命令。

$ 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. >>>

将Memcached库导入shell。

>>> import memcache

使用默认设置创建到Memcached服务的连接。

>>> client=memcache.Client([('127.0.0.1',11211)])

创建一些数据作为python字典对象。

>>> samp={"name":"poftut.com"}

将数据添加到Memcached “样本” 标记并测试15分钟。15分钟后,我们的对象将自动删除。

>>> client.set("sample",samp,time=15) True

所有的代码

>>> import memcache >>> client=memcache.Client([('127.0.0.1',11211)]) >>> samp={"name":"poftut.com"} >>> client.set("sample",samp,time=15) True

相关文章: HTTP 403禁止状态代码和修复错误

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享