西维蜀黍

【Redis】Redis 持久化(Persistence)

Background

Redis是一个内存数据库,所有的数据将保存在内存中,这与传统的MySQL、Oracle、SqlServer等关系型数据库直接把数据保存到硬盘相比,Redis的读写效率非常高。但是保存在内存中也有一个很大的缺陷,一旦断电或者宕机,内存数据库中的内容将会全部丢失。为了弥补这一缺陷,Redis提供了把内存数据持久化到硬盘文件,以及通过备份文件来恢复数据的功能,即Redis持久化机制。

  ...


【Redis】Redis 事务(Transaction)

Redis 事务(Transaction)

Redis通过 MULTIEXECDISCARDWATCH 等命令来实现事务(transaction)功能。

All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation.

事务提供了一种将多个命令请求打包,然后一次性、按顺序串行地(executed sequentially)执行多个命令的机制,并且在事务执行期间,服务器不会中断事务而改去执行其他客户端的命令请求,它会将事务中的所有命令都执行完毕,然后才去处理其他客户端的命令请求。

Either all of the commands or none are processed, so a Redis transaction is also atomic. The EXEC command triggers the execution of all the commands in the transaction, so if a client loses the connection to the server in the context of a transaction before calling the EXEC command none of the operations are performed, instead if the EXEC command is called, all the operations are performed.

以下是一个事务执行的过程,该事务首先以一个MULTI命令为开始,接着将多个命令放入事务当中,最后由 EXEC命令触发这个事务的执行,通过将这个事务提交(commit)给服务器。

  ...


【Redis】Redis Key长度与性能

Question

我们知道,在Redis中,get和set操作的time complexity均为O(1)。然而,当我们提供一个超长的key给Redis时,理论上,Redis需要花费更多内存来存储这个key。

因此,key的长度有没有可能影响Redis的性能呢?

Benchmark Script

在key为不同长度(包含的字符长度)时,Redis 的get和set操作的Performance(QPS)测试脚本

#!/bin/bash
echo "Test the length of key in Redis will influence performance..."

for (( c=1; c<=100000000000000; c=c*10 ))
do  
   echo "Current length: ${c}"
   redis-cli flushall
   redis-benchmark -n 1000000 -t set,get -r ${c} -q
done
  ...


【Linux】Crontab

区别

Cron: Cron comes from chron, the Greek prefix for ‘time’. Cron is a daemon which runs at the times of system boot.

Crontab: Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times. File location varies by operating systems.

  • crontab是一个文件(列举了要执行的cron服务的具体内容),也可以指Linux中的crontab 命令。

Cron job or cron schedule: Cron job or cron schedule is a specific set of execution instructions specifying day, time and command to execute. crontab can have multiple execution statements.

crond 系统服务

crond系统服务提供crontab命令来设定cron服务的。

在linux 系统中,由 crond这个系统服务来控制。Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的。

我们可以看下crond这个系统服务的状态:

$ sudo systemctl status cron
● cron.service - Regular background program processing daemon
     Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-03-22 16:39:57 UTC; 39s ago
       Docs: man:cron(8)
   Main PID: 4344 (cron)
      Tasks: 1 (limit: 4626)
     Memory: 348.0K
        CPU: 41ms
     CGroup: /system.slice/cron.service
             └─4344 /usr/sbin/cron -f -P

Mar 22 16:39:57 esxiubuntu systemd[1]: Started Regular background program processing daemon.
Mar 22 16:39:57 esxiubuntu cron[4344]: (CRON) INFO (pidfile fd = 3)

启动crontab服务

$ systemctl start cron

查看服务是否已经运行用

$ ps -ax | grep cron

设置cron服务的两种方法

  • 在命令行中使用 crontab 来管理服务(添加/删除相应的任务),该命令其实会将设置的cron服务存储到 /var/spool/cron/ 目录下(以用户名作为子文件夹名称)
  • 直接编辑/etc/crontab文件,以添加相应的任务
  ...


【WordPress】使用 Docker 创建 WordPress 实例

Create docker-compose.yml

Change into your project directory.

For example, if you named your directory my_wordpress:

cd my_wordpress/

Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with a volume mount for data persistence:

version: "3.9"
    
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  ...