Memcached测试记录-20230408

一、概述

Memcached(发音:mem-cash-dee)是一种流行的开源分布式缓存系统,允许您在内存中存储和检索数据,这有助于加速动态 Web 应用程序。它通常用于通过在 RAM 中缓存数据和对象来提高数据库驱动网站的性能,减少重复访问数据库的需要。

Memcached 的工作原理是将数据存储在内存中的键值对中,您的 Web 应用程序可以快速访问这些数据。这使得它比传统的基于磁盘的缓存系统快得多。Memcached 可用于多种编程语言和应用程序,包括 PHP、Java、Ruby 和 Python。

Memcached 是一种广泛使用的缓存解决方案,特别是在需要高性能数据缓存的大型 Web 应用程序中。它经常与其他缓存解决方案(如 Redis 或 APC)结合使用,为 Web 应用程序提供完整的缓存解决方案。

二、软件安装

Ubuntu自动安装命令:

sudo apt-get install memcached

使用以下命令查看安装版本

memcached --version
memcached 1.5.6

三、基本操作

1. 查看Memcached是否在运行

ps aux | grep memcached

可以使用server memcached status命令查看memcached当前运行状态

jianguoliu@jianguoliu-VirtualBox:~$ sudo service memcached status
[sudo] password for jianguoliu: 
Sorry, try again.
[sudo] password for jianguoliu: 
● memcached.service - memcached daemon
   Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2023-03-27 12:26:53 CST; 1 day 22h ago
     Docs: man:memcached(1)
 Main PID: 1041 (memcached)
    Tasks: 10 (limit: 4915)
   CGroup: /system.slice/memcached.service
           └─1041 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcache

327 12:26:53 jianguoliu-VirtualBox systemd[1]: Started memcached daemon.

配置 Memcached:默认情况下,Memcached 侦听端口 11211 并使用最大内存分配 64MB。您可以通过编辑/etc/memcached.conf文件来修改这些设置。您还可以配置 Memcached 以使用身份验证和其他高级功能。

2. 启动、停止、重启memcached

# 停止
sudo service memcached stop

# 启动
sudo service memcached start

# 重启
sudo service memcached restart

通过命令启动

$ memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid

3. 连接memcached服务

telnet <server_address> <port>

telnet localhost 11211

4. memcached数据库操作

连接服务后,可以使用以下方式设置键值对。在下面的例子中,我们设置了一个叫my_key的键,0表示我们没有使用“flags parameter”;60表示“expiration time”,使用0表示没有到期时间,单位是秒;5表示数据的长度,单位是字节(bytes)。

set my_key 0 60 5
mydata

key

flags 压缩内容 MEMCACHE_COMPRESSED

exptime 缓存时间 秒 0=永远

bytes 申请内存字节数

noreply 不需要返回数据 默认成功返回STORED,失败ERROR

value

可以使用get命令得到键对应的值

get my_key

使用add命令可以在不存在键值对时,增加键值对,使用方法如下:

add color 0 0 4
blue

命令set和命令get之间的区别

区别:

“set”命令:该命令将一个键设置为缓存中的给定值,覆盖该键的任何现有值。如果该键在缓存中不存在,则创建它。

“add”命令:该命令仅在缓存中不存在的情况下,为指定值设置键值。如果密钥已经存在于缓存中,“add”命令将失败并返回一个错误。

总结使用:

如果要设置或更新缓存中的值,无论该键是否已经存在,请使用“set”命令。

如果只在键不存在的情况下才想在缓存中设置一个值,并避免覆盖现有值,请使用“add”命令。

对于数值类型的键值对,可以使用incrdecr命令给值加1或者减1.

incr counter 1

可以使用delete命令来删除键值对

delete greeting

DELETED 删除成功

ERROR 语法错误或删除失败

NOT_FOUND key 不存在

使用以下命令删除所有的键值对

flush_all

# 远程清除方法,2S后断开连接
echo "flush_all" | nc -q 2 localhost 11211

The -q option in the nc command specifies a timeout for network connections. This option allows you to set a maximum duration for the connection attempt, after which the connection will be terminated if it hasn’t been established.

使用以下命令查看状态

stats # 系统状态

stats items

5. 断开与memcached的连接方法

使用quit命令断开与memcached的连接

quit

6. 配置memcached的内存大小

You can specify the amount of memory allocated to Memcached using the -m or --memory command-line option when starting the Memcached server.

For example, to allocate 1GB of memory to Memcached, you can use the following command:

memcached -m 1024

The -m option specifies the amount of memory to allocate in megabytes. In this example, we are allocating 1024 megabytes, which is equivalent to 1 gigabyte.

You can also specify a fraction of memory to allocate using the -M or --memory-replacement-policy option. For example, to allocate 90% of available memory to Memcached and use the least recently used (LRU) policy for evicting items from the cache when memory is full, you can use the following command:

memcached -m 0 -M 0.9 -o modern -v

The -M option specifies the fraction of memory to allocate, and the -o option specifies the memory optimization mode. The modern mode uses a combination of LRU and least frequently used (LFU) policies to optimize memory usage.

Keep in mind that the amount of memory allocated to Memcached should be carefully selected based on the available physical memory on your server and the expected workload of your application. Allocating too much memory to Memcached can lead to memory fragmentation and performance issues, while allocating too little memory can result in frequent cache evictions and decreased cache hit rates.

7. “/etc/init.d/memcached start” 和 "service memcached start"启动的区别

Both /etc/init.d/memcached start and service memcached start are used to start the Memcached service, but there are some differences between them.

/etc/init.d/memcached start is the traditional way to start a service on Linux systems. This command executes the memcached startup script located in the /etc/init.d directory, which contains the instructions for starting the Memcached service. This script can be used to start, stop, or restart the Memcached service.

service memcached start is a more modern way to start a service on Linux systems. This command uses the systemd service management system, which is the default on many Linux distributions. The service command is used to manage systemd services, and memcached is the name of the Memcached service. This command starts the Memcached service using the instructions defined in the systemd configuration file for the Memcached service.

Overall, the main difference between the two commands is the method used to start the Memcached service. The /etc/init.d/memcached start command uses the traditional startup script, while service memcached start uses the modern systemd service management system. However, both commands ultimately achieve the same result of starting the Memcached service.

8. “systemctl”与“service”的区别

systemctl and service are both command-line utilities used to manage services in Linux systems, but they have some differences.

service is a legacy command that is used to start, stop, and manage services on older Linux systems that do not use systemd. It is still supported on many modern Linux distributions, but it is being gradually replaced by systemctl.

systemctl is a more modern and comprehensive utility that is used to manage services and other system resources on Linux systems that use systemd. systemd is a system and service manager that is now the default on many modern Linux distributions.

One of the main differences between systemctl and service is that systemctl provides more features and functionality than service. For example, systemctl can be used to manage system timers, sockets, targets, and snapshots, in addition to services. systemctl also provides better integration with the Linux kernel and other system components.

Another difference is that systemctl is more standardized and portable than service. Since systemd and systemctl are now widely used on modern Linux systems, using systemctl is likely to be more consistent and reliable across different distributions.

Overall, while both systemctl and service can be used to manage services in Linux systems, systemctl is the more comprehensive and modern tool that is recommended for use on systems that use systemd.

7. 配置参数

默认配置位置:/etc/memcached.conf

-d # 以守护进程运行
-u memcache # 以该用户权限运行 默认 root
-l 127.0.0.1 # 监听来自该IP请求
-p 11211 # 监听端口
-m 64 # 分配内存 MB
-c 1024 # 允许并发连接数

安全配置:

// 放行
$ iptables -A INPUT -p tcp -s 127.0.0.1 --dport 11211 -j ACCEPT
$ iptables -A INPUT -p udp -s 127.0.0.1 --dport 11211 -j ACCEPT

// 阻止
$ iptables -I INPUT -p tcp --dport 11211 -j DROP
$ iptables -I INPUT -p udp --dport 11211 -j DROP

$ service iptables save
$ service iptables restart

# 当局域网内被放行
$ talnet 192.168.1.xxx 11211
$ status # 查看memcached状态
$ status items # 查看所有缓存项目
$ status cachedump 32 0 # 获得地址
$ get :status:264861539228401373:261588 # 读取值

四、使用过程中的问题

1. 问题描述

安装了memcached-1.4.20,装好以后通过程序客户端是可以正常存取数据的,使用telnet 127.0.0.1 11211链接也能链接的上,进入telnet命令行后,使用get, set,stats,所有命令都提示?Invalid command。

在这里插入图片描述

2. 问题解决

不要输入"ctrl+]",连上后直接输入文字。输完 telnet 127.0.0.1 11211时按一下回车,在黑屏状态时直接输 stats (此时你看不到字母的)。 再按回车!如下所示:

jianguoliu@jianguoliu-VirtualBox:~$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats

五、启动多个memched方法

可以通过启动多个守护线程的方法启动多个memcached server

memcached -d -p 11213

memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

六、 memcache与Redis的区别

Memcache and Redis are both open-source, in-memory key-value stores that are often used to improve the performance of web applications. However, there are some key differences between the two.

  1. Data Structures: Redis supports a wide range of data structures including strings, lists, sets, sorted sets, hashes, and more, whereas Memcache only supports string values.
  2. Persistence: Redis provides persistence, which means that it can save its data to disk, whereas Memcache does not provide persistence. Redis can be used as a database, whereas Memcache is a pure caching system.
  3. Clustering: Redis has built-in support for clustering, which allows multiple instances of Redis to work together to provide scalability and high availability, while Memcache requires an external solution to handle clustering.
  4. Performance: Both Redis and Memcache are fast, but Redis is generally considered to be faster than Memcache. Redis achieves this by implementing advanced techniques such as pipelining and Lua scripting.
  5. Flexibility: Redis is more flexible than Memcache due to its support for different data structures and its scripting capabilities.

In summary, Redis is more feature-rich, flexible, and powerful than Memcache. Redis can be used both as a caching system and as a database, whereas Memcache is a pure caching system. However, if you only need a simple caching system and don’t need the additional features provided by Redis, Memcache may be a better choice due to its simplicity and ease of use.

参考来源

  1. chatGPT
  2. Memcached 命令无法使用
  3. Linux Memcached 配置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值