首页 Tomcat教程Memcache安装配置

Memcache和MSM 介绍

python操作memcache

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

yum安装
# yum install memcached
# rpm -ql memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/lib/systemd/system/memcached.service
/usr/share/doc/memcached-1.4.15
/usr/share/doc/memcached-1.4.15/AUTHORS
/usr/share/doc/memcached-1.4.15/CONTRIBUTORS
/usr/share/doc/memcached-1.4.15/COPYING
/usr/share/doc/memcached-1.4.15/ChangeLog
/usr/share/doc/memcached-1.4.15/NEWS
/usr/share/doc/memcached-1.4.15/README.md
/usr/share/doc/memcached-1.4.15/protocol.txt
/usr/share/doc/memcached-1.4.15/readme.txt
/usr/share/doc/memcached-1.4.15/threads.txt
/usr/share/man/man1/memcached-tool.1.gz
/usr/share/man/man1/memcached.1.gz

# cat /usr/lib/systemd/system/memcached.service
[Service]
Type=simple
EnvironmentFile=-/etc/sysconfig/memcached
ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS

# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

前台显示看看效果
# memcached -u memcached -p 11211 -f 1.25 -vv

# systemctl start memcached

修改memcached运行参数,可以使用下面的选项修改/etc/sysconfig/memcached文件

  • -u username memcached运行的用户身份,必须普通用户
  • -p 绑定的端口,默认11211
  • -m num 最大内存,单位MB,默认64MB
  • -c num 最大连接数,缺省1024
  • -d 守护进程方式运行
  • -f 增长因子Growth Factor,默认1.25
  • -v 详细信息,-vv能看到详细信息
  • -M 内存耗尽,不许LRU
  • -U 设置UDP监听端口,0表示禁用UDP
# yum list all | grep memcached
memcached.x86_64                        1.4.15-10.el7_3.1              @base    
libmemcached.i686                       1.0.16-5.el7                   base     
libmemcached.x86_64                     1.0.16-5.el7                   base     
libmemcached-devel.i686                 1.0.16-5.el7                   base     
libmemcached-devel.x86_64               1.0.16-5.el7                   base     
memcached-devel.i686                    1.4.15-10.el7_3.1              base     
memcached-devel.x86_64                  1.4.15-10.el7_3.1              base     
opensips-memcached.x86_64               1.10.5-4.el7                   epel     
php-ZendFramework-Cache-Backend-Libmemcached.noarch
php-pecl-memcached.x86_64               2.2.0-1.el7                    epel     
python-memcached.noarch                 1.48-4.el7                     base     
uwsgi-router-memcached.x86_64           2.0.17.1-2.el7                 epel  

[root@centos7 ~]#yum list "*memcached*"
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: 
Installed Packages
memcached.x86_64                                        1.4.15-10.el7_3.1   @base
Available Packages
libmemcached.i686                                       1.0.16-5.el7        base 
libmemcached.x86_64                                     1.0.16-5.el7        base 
libmemcached-devel.i686                                 1.0.16-5.el7        base 
libmemcached-devel.x86_64                               1.0.16-5.el7        base 
memcached-devel.i686                                    1.4.15-10.el7_3.1   base 
memcached-devel.x86_64                                  1.4.15-10.el7_3.1   base 
opensips-memcached.x86_64                               1.10.5-4.el7        epel 
perl-Cache-Memcached.noarch                             1.30-8.el7          epel 
php-ZendFramework-Cache-Backend-Libmemcached.noarch     1.12.20-1.el7       epel 
php-ZendFramework-Cache-Backend-Memcached.noarch        1.12.20-1.el7       epel 
php-pecl-memcached.x86_64                               2.2.0-1.el7         epel 
python-memcached.noarch                                 1.48-4.el7          base 
uwsgi-router-memcached.x86_64                           2.0.17.1-2.el7      epel 

[root@centos8 ~]#yum list "*memcached*"
Last metadata expiration check: 0:09:46 ago on Thu 13 Feb 2020 07:14:15 PM CST.
Installed Packages
memcached.x86_64              1.5.9-2.el8          @AppStream
Available Packages
libmemcached.x86_64           1.0.18-15.el8        AppStream 
libmemcached-libs.i686        1.0.18-15.el8        AppStream 
libmemcached-libs.x86_64      1.0.18-15.el8        AppStream 
perl-Cache-Memcached.noarch   1.30-21.el8          epel      
python2-memcached.noarch      1.58-8.el8           epel      
python3-memcached.noarch      1.58-8.el8           epel      

与memcached通信的不同语言的连接器。

libmemcached提供了C库和命令行工具。

范例:在CentOS 8 上安装memcached

[root@centos8 ~]#dnf install memcached -y
[root@centos8 ~]#rpm -ql memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/lib/.build-id
/usr/lib/.build-id/89
/usr/lib/.build-id/89/217dab806ba70f88fdfbd5a731d27b0382d51e
/usr/lib/systemd/system/memcached.service
/usr/share/doc/memcached
/usr/share/doc/memcached/AUTHORS
/usr/share/doc/memcached/CONTRIBUTORS
/usr/share/doc/memcached/COPYING
/usr/share/doc/memcached/ChangeLog
/usr/share/doc/memcached/NEWS
/usr/share/doc/memcached/README.md
/usr/share/doc/memcached/new_lru.txt
/usr/share/doc/memcached/protocol.txt
/usr/share/doc/memcached/readme.txt
/usr/share/doc/memcached/storage.txt
/usr/share/doc/memcached/threads.txt
/usr/share/man/man1/memcached-tool.1.gz
/usr/share/man/man1/memcached.1.gz
[root@centos8 ~]#grep -Ev "^#|^$" /usr/lib/systemd/system/memcached.service
[Unit]
Description=memcached daemon
Before=httpd.service
After=network.target
[Service]
EnvironmentFile=/etc/sysconfig/memcached
ExecStart=/usr/bin/memcached -p ${PORT} -u ${USER} -m ${CACHESIZE} -c ${MAXCONN} $OPTIONS
PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true
PrivateDevices=true
CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
[Install]
WantedBy=multi-user.target

[root@centos8 ~]#cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1,::1"

[root@centos8 ~]#systemctl start memcached
[root@centos8 ~]#ss -ntul
Netid     State       Recv-Q       Send-Q              Local Address:Port              Peer Address:Port      
udp       UNCONN      0            0                       127.0.0.1:323                    0.0.0.0:*         
udp       UNCONN      0            0                           [::1]:323                       [::]:*         
tcp       LISTEN      0            128                       0.0.0.0:22                     0.0.0.0:*         
tcp       LISTEN      0            100                     127.0.0.1:25                     0.0.0.0:*         
tcp       LISTEN      0            128                     127.0.0.1:11211                  0.0.0.0:*         
tcp       LISTEN      0            128                          [::]:22                        [::]:*         
tcp       LISTEN      0            100                         [::1]:25                        [::]:*         
tcp       LISTEN      0            128                         [::1]:11211                     [::]:*

#修改端口绑定的IP为当前主机的所有IP
[root@centos8 ~]#vim /etc/sysconfig/memcached 
[root@centos8 ~]#cat  /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
#OPTIONS="-l 127.0.0.1,::1"
OPTIONS=""
[root@centos8 ~]#
[root@centos8 ~]#systemctl restart memcached.service 
[root@centos8 ~]#ss -ntul
Netid     State       Recv-Q       Send-Q              Local Address:Port              Peer Address:Port      
udp       UNCONN      0            0                       127.0.0.1:323                    0.0.0.0:*         
udp       UNCONN      0            0                           [::1]:323                       [::]:*         
tcp       LISTEN      0            128                       0.0.0.0:22                     0.0.0.0:*         
tcp       LISTEN      0            100                     127.0.0.1:25                     0.0.0.0:*         
tcp       LISTEN      0            128                       0.0.0.0:11211                  0.0.0.0:*         
tcp       LISTEN      0            128                          [::]:22                        [::]:*         
tcp       LISTEN      0            100                         [::1]:25                        [::]:*         
tcp       LISTEN      0            128                          [::]:11211                     [::]:*         

协议

查看/usr/share/doc/memcached-1.4.15/protocol.txt

编译安装
[root@centos7 ~]#yum –y install gcc  libevent-devel 
[root@centos7 ~]#wget http://memcached.org/files/memcached-1.5.22.tar.gz
[root@centos7 ~]#tar xvf  memcached-1.5.22.tar.gz 
[root@centos7 ~]#cd memcached-1.5.22/
[root@centos7 ~]#./configure  --prefix=/apps/memcached
[root@centos7 ~]#make && make install 
[root@centos7 ~]#tree /apps/memcached/
/apps/memcached/
├── bin
│?? └── memcached
├── include
│?? └── memcached
│??     └── protocol_binary.h
└── share
    └── man
        └── man1
            └── memcached.1

6 directories, 3 files
[root@centos7 ~]#echo 'PATH=/apps/memcached/bin:$PATH' > /etc/profile.d/memcached.sh
[root@centos7 ~]#. /etc/profile.d/memcached.sh
[root@centos7 ~]#useradd -r -s /sbin/nologin memcached
[root@centos7 ~]#memcached -u memcached  -m 2048 -c 65536 &
[root@centos7 ~]#ss -ntl
State      Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              
LISTEN     0      100                 127.0.0.1:25                                      *:*                  
LISTEN     0      128                         *:11211                                   *:*                  
LISTEN     0      128                         *:22                                      *:*                  
LISTEN     0      100                     [::1]:25                                   [::]:*                  
LISTEN     0      128                      [::]:11211                                [::]:*                  
LISTEN     0      128                      [::]:22                                   [::]:*                  
[root@centos7 ~]#
使用memcached
命令

五种基本 memcached 命令执行最简单的操作。这些命令和操作包括:

  • set
  • add
  • replace
  • get
  • delete
前三个命令是用于操作存储在 memcached 中的键值对的标准修改命令,都使用如下所示的语法:
command <key> <flags> <expiration time> <bytes>
<value>

参数说明如下:
command set/add/replace
key     key 用于查找缓存值
flags     可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息
expiration time     在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)
bytes     在缓存中存储的字节点
value     存储的值(始终位于第二行)

范例:

# yum install telnet
# telnet locahost 11211
stats

add mykey 1 60 4   
test
STORED
get mykey
VALUE mykey 1 4
test
END
set mykey 1 60 5
test1
STORED
get mykey
VALUE mykey 1 5
test1
END
delete  mykey1
DELETED
get mykey1
END
flush_all
OK
get key3
END
quit 

add key flags exptime bytes , 增加key,过期时间为秒,bytes为存储数据的字节数

本文链接:https://www.yunweipai.com/35191.html

Memcache和MSM 介绍

python操作memcache

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部