1. 程式人生 > >redis簡單使用示例(附程式碼)

redis簡單使用示例(附程式碼)

輸入使用者名稱、密碼

密碼加密

判斷redis中是否記錄了使用者名稱,如果有則成功

如果redis中沒有使用者名稱,則到mysql中查詢

從mysql中查詢成功後,將使用者名稱記錄到redis中

 

# coding=utf8
from RedisHelper import *
from MysqlHelper import *
from pwdUtl import pwdUtl

name = input("請輸入使用者:")
pwd = input("輸入密碼:")

pwd2 = pwdUtl(pwd)
try:
    # 查詢redis中是否有此資料
    r = RedisHelper("127.0.0.1", 6379)
    # 判斷redis中是否有這個使用者和密碼
    # 此處有一個問題就是使用者名稱可能重複嗎,答案是不會,
    # 在註冊時,會用mysql語句判斷,將此時資料庫儲存的是不重複的暱稱和密碼
    if not r.get(name):
        sel_sql = "select passwd from python3 where name=%s"
        m = MysqlHelper("localhost", 3306, 'python3', 'root', 'mysql')
        userpwd = m.fetchone(sel_sql, [name])[0]
        if userpwd:
            print("使用者名稱錯誤")
        else:
            # 密碼存在時,將資料存入redis資料庫,實現了快取
            r.set(name, userpwd)
            if userpwd == pwd2:
                print("密碼正確")
            else:
                print("密碼錯誤")
    else:
        r_pwd = r.get(name)
        r_pwd = r_pwd.decode('utf8')
        if r_pwd == pwd2:
            print("密碼正確")
        else:
            print("密碼錯誤")
except Exception as e:
    print(e)

# MysqlHelper.py	

# coding=utf-8
import pymysql
from redis import *


class MysqlHelper:

    def __init__(self, host='localhost', port=3306, db='test2', user='root', passwd='mysql', charset='utf8'):
        self.conn = pymysql.connect(
            host=host, port=port, db=db, user=user, passwd=passwd, charset=charset)

    def insert(self, sql, params):
        return self.__cud(sql, params)

    def update(self, sql, params):
        return self.__cud(sql, params)

    def delete(self, sql, params):
        return self.__cud(sql, params)

    def __cud(self, sql, params=[]):
        # try:
        cs1 = self.conn.cursor()
        rows = cs1.execute(sql, params)
        self.conn.commit()
        cs1.close()
        self.conn.close()
        return rows
        # except Exception,e:
        #     print e
        #     self.conn.rollback()

    def fetchone(self, sql, params=[]):
        # try:
        cs1 = self.conn.cursor()
        cs1.execute(sql, params)
        row = cs1.fetchone()
        cs1.close()
        self.conn.close()
        return row
        # except Exception as e:
        #     print(e)

    def fetchall(self, sql, params):
        # try:
        cs1 = self.conn.cursor()
        cs1.execute(sql, params)
        rows = cs1.fetchall()
        cs1.close()
        self.conn.close()

        return rows


class RedisHelper:

    def __init__(self, host='localhost', port=6379):
        self.redis = StrictRedis(host, port)

    def get(self, key):
        return self.redis.get(key)

    def set(self, key, value):
        self.redis.set(key, value)
# RedisHelper.py	# coding=utf8
from redis import *


class RedisHelper(object):

    def __init__(self, host, port):
        self.__redis = StrictRedis(host=host, port=port)
        # 初始化函式不用寫返回值,預設返回self本身

    def set(self, key, value):
        self.__redis.set(key, value)

    def get(self, key):
        return self.__redis.get(key)
# pwdUtl.py	# coding=utf8
from hashlib import sha1


def pwdUtl(pwd):
    '''加密過程'''
    # 得到加密器
    s1 = sha1()
    # 載入要加密資料
    s1.update(pwd.encode('utf-8'))
    # 執行加密
    hpwd = s1.hexdigest()
    return hpwd