<commit_before># -*- coding: utf8 -*-

import requests
from config import REDIS, MYSQL, PLUGINS
from random import choice
from torndb import IntegrityError, Connection
from utils.tool import logger, ParseRedis, ParseMySQL, get_today, ListEqualSplit, md5, BaiduIncludedCheck


class CacheManager(object):


    def __init__(self, timeout=5):
        self._info = ParseRedis(REDIS)
        self.index = "EauDouceBlog"
        if isinstance(self._info, dict):
            from redis import Redis
            self.redis = Redis(host=self._info.get("host", "localhost"), port=self._info.get("port", 6379), db=self._info.get("db", 0), password=self._info.get("password", None), socket_timeout=timeout)
        else:
            from rediscluster import StrictRedisCluster
            self.redis = StrictRedisCluster(startup_nodes=self._info, decode_responses=True, socket_timeout=timeout)
        self.mysql = Connection(
                    host     = "%s:%s" %(ParseMySQL(MYSQL).get('Host', '127.0.0.1'), ParseMySQL(MYSQL).get('Port', 3306)),
                    user     = ParseMySQL(MYSQL).get('User', 'root'),
                    password = ParseMySQL(MYSQL).get('Password'),
                    database = ParseMySQL(MYSQL).get('Database'),
                    time_zone= ParseMySQL(MYSQL).get('Timezone','+8:00'),
                    charset  = ParseMySQL(MYSQL).get('Charset', 'utf8'),
                    connect_timeout=3,
                    max_idle_time=2)

    def setKey(self, key):
        return "{}_{}".format(self.index, key)

    def cacheIndex(self):
        
        sql  = "SELECT id,title,create_time,update_time,tag,author,catalog FROM blog_article"
        data = self.mysql.query(sql)

        for blog in data:
            #print blog.id, blog.title, blog.create_time, blog.update_time, blog.tag, blog.catalog, blog.author
            key = "%s_%d" %(self.index, blog.id)
            for k,v in blog.iteritems():
                if k == "id": continue
                self.redis.hset(key, k, v)
            self.redis.expire(key, 1200)

    def post_cache_blog(self, blogId):
        key = self.setKey(blogId)
        sql = "SELECT id,title,content,create_time,update_time,tag,catalog,sources,author FROM blog_article WHERE id=%s" %blogId
        data= self.mysql.get(sql)
        return self.redis.set(key, json.dumps(data))

    def get_cache_blog(self, blogId):
        key = self.setKey(blogId)
        if self.redis.exists(key):
            return json.loads(self.redis.get(key))
        else:
            return False


class BaseApiManager(object):


    def __init__(self):
        self.mysql = Connection(
                    host     = "%s:%s" %(ParseMySQL(MYSQL).get('Host', '127.0.0.1'), ParseMySQL(MYSQL).get('Port', 3306)),
                    user     = ParseMySQL(MYSQL).get('User', 'root'),
                    password = ParseMySQL(MYSQL).get('Password'),
                    database = ParseMySQL(MYSQL).get('Database'),
                    time_zone= ParseMySQL(MYSQL).get('Timezone','+8:00'),
                    charset  = ParseMySQL(MYSQL).get('Charset', 'utf8'),
                    connect_timeout=3,
                    max_idle_time=2)

    @property
    def RegisteredUser(self):
        "返回本地已注册的用户名列表"

        sql = "SELECT lauth_username FROM user_lauth"
        logger.info("query registered user list SQL: {}".format(sql))
        try:
            data = self.mysql.query(sql)
        except Exception,e:
            logger.error(e, exc_info=True)
            logger.warn("get user list error, return false")
            return False
        else:<commit_msg>mysql write read<commit_after># -*- coding: utf8 -*-

import requests
from config import REDIS, MYSQL, PLUGINS
from random import choice
from torndb import IntegrityError, Connection
from utils.tool import logger, ParseRedis, ParseMySQL, get_today, ListEqualSplit, md5, BaiduIncludedCheck


class CacheManager(object):


    def __init__(self, timeout=5):
        self._info = ParseRedis(REDIS)
        self.index = "EauDouceBlog"
        if isinstance(self._info, dict):
            from redis import Redis
            self.redis = Redis(host=self._info.get("host", "localhost"), port=self._info.get("port", 6379), db=self._info.get("db", 0), password=self._info.get("password", None), socket_timeout=timeout)
        else:
            from rediscluster import StrictRedisCluster
            self.redis = StrictRedisCluster(startup_nodes=self._info, decode_responses=True, socket_timeout=timeout)
        #读写mysql连接
        self.mysql = Connection(
                    host     = "%s:%s" %(ParseMySQL(MYSQL).get('Host', '127.0.0.1'), ParseMySQL(MYSQL).get('Port', 3306)),
                    user     = ParseMySQL(MYSQL).get('User', 'root'),
                    password = ParseMySQL(MYSQL).get('Password'),
                    database = ParseMySQL(MYSQL).get('Database'),
                    time_zone= ParseMySQL(MYSQL).get('Timezone','+8:00'),
                    charset  = ParseMySQL(MYSQL).get('Charset', 'utf8'),
                    connect_timeout=3,
                    max_idle_time=2)
        self.mysql_write= self.mysql
        self.mysql_read = self.mysql

    def setKey(self, key):
        return "{}_{}".format(self.index, key)

    def cacheIndex(self):
        
        sql  = "SELECT id,title,create_time,update_time,tag,author,catalog FROM blog_article"
        data = self.mysql.query(sql)

        for blog in data:
            #print blog.id, blog.title, blog.create_time, blog.update_time, blog.tag, blog.catalog, blog.author
            key = "%s_%d" %(self.index, blog.id)
            for k,v in blog.iteritems():
                if k == "id": continue
                self.redis.hset(key, k, v)
            self.redis.expire(key, 1200)

    def post_cache_blog(self, blogId):
        key = self.setKey(blogId)
        sql = "SELECT id,title,content,create_time,update_time,tag,catalog,sources,author FROM blog_article WHERE id=%s" %blogId
        data= self.mysql.get(sql)
        return self.redis.set(key, json.dumps(data))

    def get_cache_blog(self, blogId):
        key = self.setKey(blogId)
        if self.redis.exists(key):
            return json.loads(self.redis.get(key))
        else:
            return False


class BaseApiManager(object):


    def __init__(self):
        self.mysql = Connection(
                    host     = "%s:%s" %(ParseMySQL(MYSQL).get('Host', '127.0.0.1'), ParseMySQL(MYSQL).get('Port', 3306)),
                    user     = ParseMySQL(MYSQL).get('User', 'root'),
                    password = ParseMySQL(MYSQL).get('Password'),
                    database = ParseMySQL(MYSQL).get('Database'),
                    time_zone= ParseMySQL(MYSQL).get('Timezone','+8:00'),
                    charset  = ParseMySQL(MYSQL).get('Charset', 'utf8'),
                    connect_timeout=3,
                    max_idle_time=2)
        self.mysql_write= self.mysql
        self.mysql_read = self.mysql

    @property
    def RegisteredUser(self):
        "返回本地已注册的用户名列表"

        sql = "SELECT lauth_username FROM user_lauth"
        logger.info("query registered user list SQL: {}".format(sql))
        try:
            data = self.mysql.query(sql)
        except Exception,e:
            logger.error(e, exc_info=True)
            logger.warn("get user list error, return false")
            return False
        else:
{'fraction_non_alphanumeric': 0.12797297297297297, 'fraction_numerical': 0.011891891891891892, 'mean_word_length': 2.268992932862191, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5347098', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2257, 'n_words': 446}