Beispiel #1
0
def set_cache(webm_data):
    """
    :param webm_data: dict with data from DB
    :return: True when successful and False otherwise
    """
    # print("setting data:", webm_data)
    id_ = webm_data.get('id', None)  # TODO: check its schema
    if id_:
        r_type = r.type('cleanwebm:' + id_)
        if r_type == 'none':
            r.hmset('cleanwebm:' + id_, webm_data)
            r.rpush('webmlist', id_)
            return True
    return False
Beispiel #2
0
def incr_views(ip, md5):
    """
    :return: True if increased, False if failed
    """
    id_ = get_dirty_cache(md5)
    if id_ is not None and id_ != "delayed":
        r_type = r.type('cleanwebm:' + id_)
        print(id_,
              r_type)  # TODO: Если нет в кэше, загрузить и увеличить счетчик.
        if r_type == 'hash':
            r.hincrby('cleanwebm:' + id_, 'views')
            r.setex("views:{}:{}".format(ip, id_), 600, 'v')
            return True
        else:
            # Get from DB
            pass
    return False
Beispiel #3
0
def save_webm_to_db(id_, session):
    """
    Note that it does not commit to session
    """
    r_type = r.type('cleanwebm:' + id_)
    try:
        if r_type == 'hash':
            data = r.hgetall('cleanwebm:' + id_)
            webm = session.query(WEBM).get(id_)
            webm.views = data['views']
            webm.likes = data['likes']
            webm.dislikes = data['dislikes']
            session.commit()  # TODO make bulk
        else:
            print('Not hash')
    except:
        print("Unexpected error while saving")
    del_clean_cache(id_)
Beispiel #4
0
def check_ip_viewed(md5, ip):
    """
    return TTL of key if viewed and return False is expired or didn't viewed
    """
    id_ = get_dirty_cache(md5)
    if id_ is not None and id_ != "delayed":
        ttl = r.ttl("views:{}:{}".format(ip, id_))
        if ttl == -2:  # expired
            return False
        else:
            return ttl
    return False
Beispiel #5
0
def setup_module():
    r.flushall()
    # Setup DB
    global session, engine, api
    # Connect to the database and create the schema within a transaction
    engine = create_engine('sqlite:///test_db.sqlite3')  # TODO: move to config
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    api = create_app('sqlite:///test_db.sqlite3')

    # If you want to insert fixtures to the DB, do it here
    clean = WEBM(id_="dbcleannone")
    clean2 = WEBM(id_="dbclean1", screamer_chance=1)
    clean3 = WEBM(id_="dbclean0_5", screamer_chance=0.5)

    dirty = DirtyWEBM(md5="dbdirtynone", webm_id="dbcleannone")
    dirty_dup = DirtyWEBM(md5="dbdirtynone_dup", webm_id="dbcleannone")
    dirty2 = DirtyWEBM(md5="dbdirty0_5", webm_id="dbclean0_5")
    dirty3 = DirtyWEBM(md5="dbdirty1", webm_id="dbclean1")
    session.add_all([clean, clean2, clean3, dirty, dirty_dup, dirty2, dirty3])
    session.commit()
Beispiel #6
0
def get_clean_cache(id_):
    """
    :param id_: md5 cache of stripped webm
    :return: dict with data from cache or None
    """
    r_type = r.type('cleanwebm:' + id_)

    if r_type == 'hash':
        cache = r.hgetall('cleanwebm:' + id_)
        # print("clean_cachce:", cache['screamer_chance'])
        if cache.get(
                'screamer_chance',
                None) == 'None':  # Because of redis-py (nil) value casting
            cache['screamer_chance'] = None
        # TODO: Convert from strings types to floats and ints
        return cache
    else:
        #print("Data from DB")
        session = Session()
        webm_data = session.query(WEBM).get(
            id_)  # Assuming it will be there anyway
        set_cache(webm_data.to_dict())
        return webm_data.to_dict()
Beispiel #7
0
def teardown_module():
    global api
    del api  # Hope it works
    r.flushall()
    engine.dispose()
    drop_database(engine.url)
Beispiel #8
0
def pop_webm_from_redis_list():
    return r.lpop('webmlist')
Beispiel #9
0
def del_all_clean_cache():
    """Delete all cache in CLEANWEBM: namespace"""
    for key in r.scan_iter("cleanwebm:*"):
        print('deleted clean: {}'.format(key))
        r.delete(key)
Beispiel #10
0
def del_all_dirty_cache():
    """Delete all cache in DIRTYWEBM: namespace"""
    for key in r.scan_iter("dirtywebm:*"):
        print('deleted dirty: {}'.format(key))
        r.delete(key)
Beispiel #11
0
def del_clean_cache(id_):
    r.delete('cleanwebm:' + id_)
Beispiel #12
0
def del_dirty_cache(md5):
    r.delete('dirtywebm:' + md5)
Beispiel #13
0
def set_cache_delayed(md5):
    # r.set('dirtywebm:' + md5, 'delayed')
    r.setex('dirtywebm:' + md5, 7200, 'delayed')  # 2 hours
Beispiel #14
0
def set_dirty_cache(md5, id_):
    r.set('dirtywebm:' + md5, id_)
Beispiel #15
0
def like_webm(md5, ip, action):
    # Если все прошло удачно - вернуть словарь с количеством лайков/дизлайков, иначе вернуть None
    # в ip:127.0.0.1 хранится хэш вида {viewed:{ISO TIME} action:{nil, like or dislike}}
    id_ = get_dirty_cache(md5)
    if id_ is not None and id_ != "delayed":
        r_type = r.type('cleanwebm:' + id_)
        if r_type == 'hash':
            ip_action = r.hget('ip:' + ip + ":" + id_, 'action')
            # Убираем лайк
            if action == ip_action:
                r.hincrby('cleanwebm:' + id_, action + 's', -1)
                r.hset('ip:' + ip + ":" + id_, 'action', None)
                action = None
            # Спокойно ставим лайк
            elif ip_action is None:
                r.hincrby('cleanwebm:' + id_, action + 's')
                r.hset('ip:' + ip + ":" + id_, 'action', action)
            # Убираем одно действие и ставим другое
            else:
                r.hincrby('cleanwebm:' + id_, action + 's')
                r.hincrby('cleanwebm:' + id_, ip_action + 's', -1)
                r.hset('ip:' + ip + ":" + id_, 'action', action)
            result = r.hmget('cleanwebm:' + id_, ('likes', 'dislikes'))
            return {
                'md5': md5,
                'id': id_,
                'likes': result[0],
                'dislikes': result[1],
                'action': action
            }
    return
Beispiel #16
0
def get_dirty_cache(md5):
    """
    :return: 'delayed' message, clean_md5 or None
    """
    clean_md5 = r.get('dirtywebm:' + md5)
    return clean_md5