使用python批量删除redis key

意犹未尽 / 2024-02-27 / 原文

 

比如我的业务。刚上线默认为超级管理员新增权限

-- 请导出id 用于清缓存 svc格式 请注意分页 需要导出全部
select CONCAT('@rbac/ent/aclgr/',e.id) as 需要清理缓存的rediskey from ent_rbac_group e where
not exists(select p.`groupid` from ent_rbac_group_permission p where e.id=p.`groupid` and p.`targetid`='transfer::FileTransfer') and ( `name` ='超级管理员')



INSERT IGNORE INTO ent_rbac_group_permission (groupid, targetid, targettype, allow)
select e.id, 'transfer::FileTransfer', 1, 1 from ent_rbac_group e where
not exists(select p.`groupid` from ent_rbac_group_permission p where e.id=p.`groupid` and p.`targetid`='transfer::FileTransfer') and ( `name` ='超级管理员');

svc格式

 

py脚本

#!/usr/bin/python
#-*- coding:utf-8 -*-



import redis
import sys
reload(sys)
sys.setdefaultencoding("utf-8")


def redis_delbyfile(filepath):
    host = 'xxx'
    password = 'xxx'
    Pool = redis.ConnectionPool(host=host, port=26379, password=password, socket_connect_timeout=5)
    r = redis.StrictRedis(connection_pool=Pool)
    with open(filepath, 'r') as f:
        contents = f.readlines()
    for content in contents:
        content = str(content.strip('\r\n').strip('\n'))
        print(content)
        result = r.delete(content)
        print(repr(result))


if __name__ == '__main__':
    if len(sys.argv) == 2:
        filepath = sys.argv[1]
        redis_delbyfile(filepath)

1.安装环境

pip install reload

pip install redis

执行

python3 {脚本path}.py {svcpath}.csv

 

常见问题

安装python脚本

1.先检测下是否安装python:在命令行里输入命令

ls -l /usr/bin | grep python

如果没有则正常安装

未安装redis模块

Traceback (most recent call last):
  File "/Users/liqiang/Desktop/redis_exec.py", line 6, in <module>
    import redis
ModuleNotFoundError: No module named 'redis'

安装redis模块

pip install redis

1.如果未安装pip则先安装pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py

or

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

pip安装失败

➜  Desktop python3 get-pip.py                                     
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-brew-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.
    
    If you wish to install a non-brew packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

推荐的做法是创建一个虚拟环境来管理您的Python包,而不是尝试在全局环境中安装包。

python3 -m venv myenv (这将在当前目录下创建一个名为myenv的新虚拟环境。)

source myenv/bin/activate (nix/Linux 激活)

myenv\Scripts\activate (windows 激活)

再执行 

pip install redis 

(myenv) ➜  Desktop pip install redis 
Collecting redis
  Downloading redis-5.0.1-py3-none-any.whl.metadata (8.9 kB)
Downloading redis-5.0.1-py3-none-any.whl (250 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 250.3/250.3 kB 1.1 MB/s eta 0:00:00
Installing collected packages: redis
Successfully installed redis-5.0.1
(myenv) ➜  Desktop 

pyhon3执行

需要删除 

import importlib
importlib.reload(sys)
sys.setdefaultencoding("utf-8")
 

如何退出和进入环境

要进入和退出虚拟环境(myenv),您需要使用以下命令:

进入虚拟环境:

在终端或命令提示符下,使用以下命令进入虚拟环境:

对于 Unix/Linux 系统:

source myenv/bin/activate

对于 Windows 系统:

myenv\Scripts\activate

退出虚拟环境:

在虚拟环境中工作完成后,您可以通过运行以下命令来退出虚拟环境:

deactivate

这样就可以退出虚拟环境,并返回到全局环境中。

请注意,在退出虚拟环境之前,您将无法使用deactivate命令。这个命令只在虚拟环境中有效。