#coding:utf-8 from _db import McCache from zsite import Zsite PO_SHOW_ZSITE_CHANNEL = ( 10036807, 10036808, 10036809, 10036810, 10036811, 10036812, 10036813, 10036814, ) mc_po_show_zsite_channel = McCache('PoShowZsiteChannel:%s') @mc_po_show_zsite_channel('') def po_show_zsite_channel(): return tuple( zip(PO_SHOW_ZSITE_CHANNEL, [i.name for i in Zsite.mc_get_list(PO_SHOW_ZSITE_CHANNEL)]))
#!/usr/bin/env python # -*- coding: utf-8 -*- from state import STATE_RM, STATE_ACTIVE from _db import McModel, McCache, cursor_by_table, McCacheA, McCacheM from mq import mq_client from zkit.feed_merge import MAXINT, merge_iter as _merge_iter from cid import CID_REC PAGE_LIMIT = 50 mc_feed_iter = McCacheM('FeedIter:%s') mc_feed_tuple = McCacheM('F%s') mc_feed_rt_id = McCache('R%s') cursor = cursor_by_table('feed') class Feed(McModel): pass def feed_new(id, zsite_id, cid, rid=0): cursor.execute( 'insert into feed (id, zsite_id, cid) values (%s,%s,%s) on duplicate key update id=id', (id, zsite_id, cid) ) cursor.connection.commit() mc_feed_iter.delete(zsite_id) return id def feed_rm(id):
class Vote(Model): pass vote_up_count = McNum( lambda po_id: Vote.where(po_id=po_id, state=STATE_UP).count(), 'VoteUpCount:%s') vote_down_count = McNum( lambda po_id: Vote.where(po_id=po_id, state=STATE_DOWN).count(), 'VoteDownCount:%s') vote_count = McNum(lambda po_id: vote_up_count(po_id) - vote_down_count(po_id), 'VoteCount:%s') mc_vote_state = McCache('VoteState.%s') @mc_vote_state('{user_id}_{po_id}') def _vote_state(user_id, po_id): v = Vote.get(user_id=user_id, po_id=po_id) if v: return v.state return 0 def vote_state(user_id, po_id): if not user_id: return 0 return _vote_state(user_id, po_id)
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import Model, McModel, McCache, McCacheA from state import STATE_APPLY from mail import render_template mc_top_notice_id_by_user_id = McCache('TopNoticeIdByUserId.%s') class TopNotice(McModel): pass @mc_top_notice_id_by_user_id('{user_id}') def top_notice_id_by_user_id(user_id): for i in TopNotice.where(user_id=user_id, state=1).order_by('id desc')[:1]: return i.id return 0 def top_notice_by_user_id(user_id): id = top_notice_id_by_user_id(user_id) if id: return TopNotice.mc_get(id) def top_notice_new(user_id, cid, rid): o = TopNotice(user_id=user_id, cid=cid, rid=rid, state=1) o.save() mc_top_notice_id_by_user_id.set(user_id, o.id) return o
(CID_PHOTO, 'photo', '图片', '张'), (CID_VIDEO, 'video', '视频', '场'), (CID_AUDIO, 'audio', '音乐', '段'), (CID_EVENT, 'event', '活动', '次'), (CID_REC, 'recommend', '推荐', '次'), ) PO_CID = tuple([ i[0] for i in PO_CN_EN ]) PO_SHARE_FAV_CID = set([i[0] for i in PO_CN_EN if i[0]!=CID_REC]) PO_EN = dict((i[0], i[1]) for i in PO_CN_EN) PO_CN = dict((i[0], i[2]) for i in PO_CN_EN) PO_COUNT_CN = dict((i[0], i[3]+i[2]) for i in PO_CN_EN) mc_htm = McCache('PoHtm.%s') class Po(McModel, ReplyMixin): @property def txt(self): cid = self.cid if cid == CID_WORD: return self.name_ elif cid in (CID_EVENT_NOTICE, CID_REC): return self.name_ elif cid == CID_ANSWER: return txt_get(self.id) or self.name_ else:
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import cursor_by_table, McModel, McLimitA, McCache, McNum, McCacheA from state import STATE_APPLY, STATE_SECRET, STATE_ACTIVE from po import po_new, Po, STATE_ACTIVE, po_list_count from model.po import po_rm, reply_rm_if_can from model.reply import Reply from cid import CID_REC mc_po_recommend_id_by_rid_user_id = McCache('PoRecommendIdByRidUserId:%s') mc_reply_id_by_recommend = McCache('ReplyIdByRecommend:%s') class RecRep(McModel): pass def rm_rec_po_by_po_id(user_id,id): ''' DANGEROUS USE WITH CAUTION ''' for po in Po.where('cid = %s and rid=%s',CID_REC,id): po_rm(po.user_id,po.id) from mq import mq_client mq_rm_rec_po_by_po_id = mq_client(rm_rec_po_by_po_id) def po_recommend_new(rid, user_id, name, reply_id=None): '''新建推荐''' #判定? rec_po = Po.mc_get(rid) if not rec_po: return
cursor = connection.cursor() if email: cursor.execute( '''insert into `account` (name, email) values (%s,%s)''', (name, email) ) user_id = user_id_by_email(email) return user_id def user_by_id(user_id): cursor = connection.cursor() cursor.execute('select name, email from account where id=%s', user_id) user = cursor.fetchone() return user mc_user_mail = McCache("UserMail:%s") @mc_user_mail("{user_id}") def user_mail(user_id): cursor = connection.cursor() cursor.execute('select email from account where id=%s', user_id) r = cursor.fetchone() if r: return r[0] def user_id_by_email(email): cursor = connection.cursor() cursor.execute('select id from account where email=%s',email) user_id = cursor.fetchone() if user_id: user_id = user_id[0]
def spammer_new(user_id): user_id = int(user_id) redis.sadd(SPAMMER_REDIS_KEY, user_id) def spammer_rm(user_id): user_id = int(user_id) redis.srem(SPAMMER_REDIS_KEY, user_id) def is_spammer(user_id): return redis.sismember(SPAMMER_REDIS_KEY, user_id) #or redis.get(MUTE_FOR_DURATION%user_id) mc_lastest_hash = McCache('LastestHash:%s') def is_same_post(user_id, *args): m = md5() for i in args: if type(i) is not str: i = str(i) m.update(i) h = m.digest() user_id = str(user_id) if h == mc_lastest_hash.get(user_id): return True mc_lastest_hash.set(user_id, h, 60) return False
CID_MAIL_NOTICE_ALL = ( CID_BUZZ_FOLLOW, # CID_INVITE_QUESTION, CID_MAIL_DAY, CID_MAIL_MONTH, CID_MAIL_YEAR, CID_MAIL_WEEK, ) class MailNotice(Model): pass mc_mail_notice_state = McCache('MailNoticeState.%s') @mc_mail_notice_state('{user_id}_{cid}') def mail_notice_state(user_id, cid): m = MailNotice.get(user_id=user_id, cid=cid) if not m: MailNotice.raw_sql( 'insert into mail_notice (user_id, cid, state) values (%s, %s, 1) on duplicate key update state=state', user_id, cid) m = MailNotice.get(user_id=user_id, cid=cid) return m.state def mail_notice_all(user_id): return [(cid, mail_notice_state(user_id, cid))
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import Model, McModel, McCache, McCacheA, McNum, McCacheM from cid import CID_VIDEO from model.po import po_new, txt_new, is_same_post, STATE_SECRET, time_title from video_swf import video_link_by_cid_uri, VIDEO_CID2LINK_AUTOPLAY, _HTM_SWF mc_video_uri = McCache('VideoUri:%s') class Video(Model): pass @mc_video_uri('{id}') def video_uri(id): c = Video.raw_sql('select uri from video where id=%s', id) r = c.fetchone() if r: return r[0] return '' def video_link_autoplay(cid, id): return video_link_by_cid_uri(cid, video_uri(id), VIDEO_CID2LINK_AUTOPLAY) def video_htm_autoplay(cid, id): return _HTM_SWF % video_link_autoplay(cid, id)
lambda city_pid: event_end_by_city_pid_query(city_pid).count(), 'EventEndCountByCityPid.%s') mc_event_joiner_feedback_normal_id_list = McCacheA( 'EventJoinerReveiwIdList:%s') event_list_join_by_user_id_query = lambda user_id: EventJoiner.where( user_id=user_id).where('state>=%s' % EVENT_JOIN_STATE_YES) event_join_count_by_user_id = McNum( lambda user_id: event_list_join_by_user_id_query(user_id).count(), 'EventJoinCountByUserId.%s') mc_event_id_list_join_by_user_id = McLimitA('EventIdListJoinByUserId.%s', 128) mc_event_joiner_id_get = McCache('EventJoinerIdGet.%s') mc_event_joiner_user_id_list = McCacheA('EventJoinerUserIdList.%s') mc_event_joining_id_list = McCacheA('EventJoiningIdList.%s') mc_event_joined_id_list = McCacheA('EventJoinedIdList.%s') event_to_review_count_by_zsite_id = McNum( lambda zsite_id: Event.where(state=EVENT_STATE_TO_REVIEW, zsite_id=zsite_id).count(), 'EventToReviewCountByZsiteId:%s') event_count_by_zsite_id = McNum( lambda zsite_id, can_admin: Event.where(zsite_id=zsite_id).where( 'state between %s and %s', EVENT_STATE_REJECT, EVENT_VIEW_STATE_GET[ can_admin]).count(), 'EventCountByZsiteId.%s')
def po_question_new(user_id, name, txt, state, zsite_id): if not name and not txt: return name = name or time_title() if not is_same_post(user_id, name, txt, zsite_id): m = po_new(CID_QUESTION, user_id, name, state, zsite_id=zsite_id) if m: txt_new(m.id, txt) if state > STATE_SECRET: m.feed_new() return m mc_answer_id_get = McCache('AnswerIdGet.%s') answer_count = McNum( lambda id: Po.where(rid=id).where('state>%s', STATE_RM).count(), 'AnswerCount:%s') @mc_answer_id_get('{user_id}_{question_id}') def answer_id_get(user_id, question_id): for i in Po.where(user_id=user_id, rid=question_id).where( 'cid in (%s,%s)' % (CID_WORD, CID_ANSWER)).where( 'state>%s', STATE_RM).col_list(1, 0): return i return 0 def answer_word2note(po):
from state import STATE_RM, STATE_APPLY, STATE_SECRET, STATE_ACTIVE from cid import CID_PO, CID_SITE, CID_COM from zkit.attrcache import attrcache from user_mail import mail_by_user_id from mail import rendermail from buzz_reply import mq_buzz_po_reply_rm, mq_buzz_po_reply_new REPLY_STATE = ( STATE_RM, STATE_APPLY, STATE_ACTIVE, ) mc_reply_id_list = McLimitA('ReplyIdList:%s', 512) mc_reply_id_list_reversed = McLimitA('ReplyIdListReversed:%s', 512) mc_reply_count = McCache('ReplyCount:%s') #mc_reply_in_1h = McCache('ReplyInOneHour.%s') mc_reply_zsite_id_list = McCacheA('ReplyZsiteIdList:%s') class ReplyMixin(object): reply_cursor = cursor_by_table('reply') @mc_reply_zsite_id_list('{self.cid}_{self.id}') def reply_zsite_id_list(self): id_set = set() id_set.update(i.user_id for i in self.reply_list()) return list(id_set) def reply_new(self, user, txt, state=STATE_ACTIVE, create_time=None): from zsite import user_can_reply
txt = txt.replace('\r\n', '\n').replace('\r', '\n').rstrip() t = Txt.get(id) if t is None: Txt(id=id, txt=txt).save() elif txt != t.txt: c = cursor_by_table('txt_history') c.execute( 'insert delayed into txt_history (rid,txt,create_time) values (%s,%s,%s)', (id, t.txt, int(time()))) c.connection.commit() t.txt = txt t.save() mc_flush(id, txt) mc_txt = McCache('Txt:%s') @mc_txt('{id}') def txt_get(id): c = Txt.raw_sql('select txt from txt where id=%s', id) r = c.fetchone() if r: return r[0] return '' def txt_bind(o_list): r = mc_txt.get_dict(i.id for i in o_list) for i in o_list: iid = i.id
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import Model, McModel, McCache, cursor_by_table, McCacheA, McLimitA, McNum, McCacheM from po import Po from cid import CID_PRODUCT, CID_COM from zsite_show import zsite_show_new class ProductShow(McModel): pass mc_product_show_get = McCache('ProductNewGet:%s') mc_product_show_id_list = McLimitA('ProductShowIdList:%s', 512) _product_show_count = McNum(lambda x: ProductShow.where('rank>0').count(), 'ProductShowCount%s') def product_show_count(): return _product_show_count('') def product_show_new(product, rank=None): id = product.id cid = product.cid if cid != CID_PRODUCT: return zsite_show_new(product.zsite_id, CID_COM) ps = ProductShow.get_or_create(id=id) if rank is None: c = ProductShow.raw_sql('select max(rank) from product_show') r = c.fetchone()
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import McCacheA, McCache from cid import CID_EVENT from po import Po, po_new, po_note_new, po_cid_set from state import STATE_RM, STATE_ACTIVE from zsite import Zsite from model.cid import CID_EVENT, CID_EVENT_FEEDBACK, CID_EVENT_NOTICE from model.pic import pic_new_save, fs_url_jpg, fs_set_jpg from zkit.pic import pic_fit from rank import rank_po_id_list, rank_new from event import EVENT_CID, EVENT_CID_CN, EventJoiner, event_joiner_get, EVENT_JOIN_STATE_FEEDBACK_NORMAL, EVENT_JOIN_STATE_FEEDBACK_GOOD, event_joiner_feedback_normal_id_list, Event, EVENT_JOIN_STATE_END, mc_flush_feedback mc_event_feedback_id_get = McCache('EventFeedBackGet.%s') @mc_event_feedback_id_get('{user_id}_{event_id}') def event_feedback_id_get(user_id, event_id): c = Po.raw_sql( 'select id from po where rid=%s and cid=%s and user_id=%s and state>%s', event_id, CID_EVENT_FEEDBACK, user_id, STATE_RM) r = c.fetchone() if r: return r[0] return 0 def event_feedback_get(user_id, event_id): id = event_feedback_id_get(user_id, event_id) if id: return Po.mc_get(id)
OAUTH_DOUBAN, OAUTH_SINA, OAUTH_QQ, OAUTH_TWITTER, OAUTH_RENREN, ) SITE_LINK_NAME = ((OAUTH_MY, '官方网站'), (OAUTH_DOUBAN, '豆瓣小站'), (OAUTH_SINA, '新浪微博'), (OAUTH_QQ, '腾讯微博'), (OAUTH_RENREN, '人人主页')) SITE_LINK_ZSITE_DICT = dict(SITE_LINK_NAME) OAUTH2NAME = tuple((k, OAUTH2NAME_DICT[k]) for k in OAUTH_LINK_DEFAULT) mc_link_id_name = McCache('LinkIdName:%s') mc_link_id_cid = McCache('LinkIdCid:%s') mc_link_by_id = McCache('LinkById:%s') def mc_flush(zsite_id): mc_link_id_name.delete(zsite_id) mc_link_id_cid.delete(zsite_id) class ZsiteLink(Model): pass @mc_link_id_name('{zsite_id}') def link_id_name_by_zsite_id(zsite_id):
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import Model, McModel, McCache, mc, cursor_by_table from os import urandom from struct import pack, unpack from base64 import urlsafe_b64encode, urlsafe_b64decode from time import time import binascii mc_user_session = McCache('UserSession:%s') def id_binary_encode(user_id, session): user_id_key = pack('I', int(user_id)) user_id_key = urlsafe_b64encode(user_id_key)[:6] ck_key = urlsafe_b64encode(session) return '%s%s' % (user_id_key, ck_key) def user_id_by_base64(string): try: user_id = urlsafe_b64decode(string + '==') except (binascii.Error, TypeError): return 0 else: return unpack('I', user_id)[0] def id_binary_decode(session): if not session: return
#!/usr/bin/env python # -*- coding: utf-8 -*- from yajl import dumps, loads from time import time from mail import rendermail from _db import Model, McModel, McCache, McNum from kv import Kv from cid import CID_TRADE_CHARDE, CID_TRADE_WITHDRAW, CID_TRADE_PAY, CID_TRADE_DEAL, CID_TRADE_EVENT, CID_VERIFY_MONEY, CID_PAY_ALIPAY, CID_NOTICE_PAY from zsite import Zsite from user_mail import mail_by_user_id from mail import rendermail from verify import verify_new, verifyed from state import STATE_APPLY, STATE_SECRET, STATE_ACTIVE from zkit.attrcache import attrcache mc_frozen_get = McCache('FrozenBank.%s') def read_cent(cent): return ('%.2f' % (cent / 100.)).rstrip('0').rstrip('.') # Bank bank = Kv('bank', 0) def bank_view(user_id): return read_cent(bank.get(user_id)) def bank_change(user_id, cent):
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import Model, McCache from model.zsite import Zsite from model.cid import CID_USER MAIL_UNVERIFY = 40 MAIL_VERIFIED = 50 MAIL_LOGIN = 60 class UserMail(Model): pass mc_mail_by_user_id = McCache('MailByUserId.%s') #mc_mail_by_user_id_if_login = McCache('MailByUserIdIfLogin.%s') #@mc_mail_by_user_id_if_login('{user_id}') #def mail_by_user_id_if_login(user_id): # c = UserMail.raw_sql('select mail from user_mail where user_id=%s and state=%s', user_id, MAIL_LOGIN).fetchone() # if c: # return c[0] # return '' @mc_mail_by_user_id('{user_id}') def mail_by_user_id(user_id): c = UserMail.raw_sql('select mail from user_mail where user_id=%s order by state desc limit 1', user_id).fetchone() if c: return c[0] return ''
#!/usr/bin/env python # -*- coding: utf-8 -*- import re from _db import Model, McModel, McCache from collections import defaultdict from hashlib import sha256 from zsite import Zsite from config import SITE_DOMAIN, SITE_DOMAIN_SUFFIX class Url(Model): pass mc_url_by_id = McCache('UrlById<%s') mc_id_by_url = McCache('IdByUrl<%s') @mc_url_by_id('{id}') def url_by_id(id): u = Url.get(id) if u is None: return '' url = u.url return url def url_or_id(id): return url_by_id(id) or id @mc_id_by_url('{url}') def _id_by_url(url): u = Url.get(url=url) if u is None:
CREATE TABLE `wall_reply` ( `id` int(10) unsigned NOT NULL auto_increment, `wall_id` int(10) unsigned NOT NULL, `zsite_id` int(10) unsigned NOT NULL, `from_id` int(10) unsigned NOT NULL, `last_reply_id` int(10) unsigned NOT NULL default '0', `update_time` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `Index_3` (`zsite_id`,`from_id`), KEY `zsite_id` (`zsite_id`,`last_reply_id`,`update_time`) ) ENGINE=MyISAM DEFAULT CHARSET=binary; """ mc_reply_id_list_reversed = McLimitA('WallReplyIdListReversed:%s', 512) mc_reply_count = McCache('Zsite.reply_count:%s') class Wall(McModel, ReplyMixin): def zsite_id_list(self): return set([self.from_id, self.to_id]) def zsite_id_other(self, id): if id == self.from_id: return self.to_id if id == self.to_id: return self.from_id return 0 def zsite_other(self, id): return Zsite.mc_get(self.zsite_id_other(id))
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import Model, McModel, McCache from state import STATE_RM, STATE_ACTIVE from zkit.earth import place_name from zkit.attrcache import attrcache from zkit.mc_func import mc_func_get_dict class Namecard(McModel): @attrcache def place_now(self): return place_name(self.pid_now) mc_namecard_id = McCache('NamecardId.%s') @mc_namecard_id('{user_id}') def namecard_get_id(user_id): for i in Namecard.where(user_id=user_id, state=STATE_ACTIVE): return i.id return 0 def namecard_get(user_id): id = namecard_get_id(user_id) return Namecard.mc_get(id) def namecard_new(user_id,
#!/usr/bin/env python # -*- coding: utf-8 -*- from _db import Model, McModel, McCache, McCacheA, McLimitA, McNum from po import Po, PO_SHARE_FAV_CID from event import Event from state import STATE_SECRET, STATE_ACTIVE from cid import CID_EVENT from model.po_pos import po_pos_get, po_pos_set, STATE_BUZZ from zsite import Zsite from collections import defaultdict from model.buzz import mq_buzz_po_fav_new class Fav(Model): pass mc_fav_cid = McCache('FavCid.%s') @mc_fav_cid('{user_id}_{po_id}') def fav_cid(user_id, po_id): f = Fav.get(user_id=user_id, po_id=po_id) if f: return f.cid return 0 def fav_cid_dict(user_id, po_id_list): if not user_id: return defaultdict(int) arg_list = tuple((user_id, i) for i in po_id_list) key_dict = dict(('%s_%s' % (user_id, i), i) for i in po_id_list) t = mc_fav_cid.get_dict(key_dict) d = {}
4, # 知识整理 5, # 指点江山 6, # 转载收藏 ) mc_zsite_tag_id_list_by_zsite_id = McCacheA('ZsiteTagIdListByZsiteId.%s') mc_tag_id_list_by_zsite_id = McCacheA('TagIdListByZsiteId:%s') mc_tag_by_po_id = McCacheM('TagIdByPoId:%s') mc_po_id_list_by_zsite_tag_id = McLimitA('PoIdListByZsiteTagId:%s', 128) zsite_tag_count = McNum(lambda id: ZsiteTagPo.where(zsite_tag_id=id).count(), 'ZsiteTagCount:%s') zsite_tag_cid_count = McNum( lambda id, cid: ZsiteTagPo.where(zsite_tag_id=id, cid=cid).count(), 'ZsiteTagCount:%s') mc_po_id_list_by_zsite_tag_id_cid = McLimitA('PoIdListByTagIdCid:%s', 128) mc_zsite_tag_list_by_zsite_id_if_len = McCache('ZsiteTagListByZsiteIdIfLen*%s') class ZsiteTag(McModel): pass class ZsiteTagPo(McModel): pass @mc_tag_id_list_by_zsite_id('{zsite_id}') def tag_id_list_by_zsite_id(zsite_id): return ZsiteTag.where(zsite_id=zsite_id).order_by('id desc').col_list( col='tag_id')
#coding:utf-8 import _env import time from _db import connection, kv, McNum, McCache, McLimitM, McLimitA, McNum from lib.txt import cnenoverflow from time import time mc_txt_brief = McCache("TxtBrief:%s") class USER_NOTE: DEFAULT = 10 RM = 0 KV_TXT_SAVE_TIME = "TxtSaveTime:" KV_TXT = "Txt:" def txt_get(id): return kv.get(KV_TXT + str(id)) or '' def txt_set(id, txt): txt = txt.rstrip() key = KV_TXT + str(id) if txt: return kv.set(key, txt) else: kv.delete(key)
def name_uid_set( id, name, uid, table): cursor = OauthToken.raw_sql('select id from %s where id=%%s'%table, id) sql_tuple = (name, uid, id) if cursor.fetchone(): OauthToken.raw_sql('update %s set name=%%s,uid=%%s where id=%%s'%table, *sql_tuple) else: OauthToken.raw_sql( 'insert into %s (name,uid,id) values (%%s,%%s,%%s)'%table, *sql_tuple ) def oauth_token_by_oauth_id(oauth_id): s = OauthToken.raw_sql('select app_id, token_key, token_secret from oauth_token where id =%s', oauth_id).fetchone() return s mc_oauth_name_by_oauth_id = McCache('OauthNameByOauthId:%s') @mc_oauth_name_by_oauth_id('{oauth_id}') def oauth_name_by_oauth_id(app_id, oauth_id): table = OAUTH2TABLE[app_id] cursor = cursor_by_table(table) cursor.execute('select name from %s where id=%%s'%table, oauth_id) r = cursor.fetchone() if r: return r[0] def oauth_rm_by_oauth_id(oauth_id): cursor = cursor_by_table('oauth_token') cursor_new = cursor_by_table('oauth_token_backup')
REDIS_REC_USER_TAG_CAN_REC = 'Rec?%s' #用户 - 可以推荐的主题 - 分数 string REDIS_REC_USER_TAG_READED = 'Rec(%s-%s' #用户 - 主题 - 已经读过的文章 REDIS_REC_USER_PO_TO_REC = 'Rec)%s-%s' #用户 - 主题 - 可以推荐的文章的缓存 REDIS_REC_TAG_USER_IS_EMPTY = 'Rec~%s' #主题 - 已经读完了的用户 set REDIS_REC_USER_LOG = 'Rec+%s' #用exist判断文章是否已经读过 zset REDIS_REC_TAG = 'RecTag' #所有热门主题 用于没有推荐主题的时候随机推荐 REDIS_REC_TAG_ID_SCORE = 'RecTagIdScore' #所有热门主题 id score的缓存 REDIS_REC_TAG_NEW = 'Rec/%s' #话题下的新内容 set REDIS_REC_TAG_OLD = 'Rec&%s' #话题下的老内容 REDIS_REC_PO_SCORE = 'RecPoScore' #话题的积分 hset REDIS_REC_PO_TIMES = 'RecTimes' #老话题的被推荐次数 REDIS_REC_LAST_TIME = 'RecLastTime' #上次推荐话题的时间 mc_rec_is_empty = McCache('Rec!%s') REDIS_REC_USER_TAG_LIMIT = 512 REDIS_REC_PO_SHOW_TIMES = 10 inf = float('inf') ninf = float('-inf') def rec_read_user_topic_score_fav(user_id, tag_id): rec_read_user_topic_score_incr(user_id, tag_id, score=inf, tag_score=1) def rec_read_po_tag_rm(po_id, tag_id_list): p = redis.pipeline() for tag_id in tag_id_list: p.zrem(REDIS_REC_TAG_OLD%tag_id, po_id) p.srem(REDIS_REC_TAG_NEW%tag_id, po_id)
from kv import Kv from txt import txt_property, txt_new from gid import gid from uuid import uuid4 from user_session import id_binary_encode, id_binary_decode import binascii from os import urandom import time oauth_client_uri = Kv('oauth_client_uri') oauth_authorize_code = Kv('oauth_authorize_code') mc_oauth_client_id_by_user_id = McCacheA('OauthClientIdListByUserId.%s') mc_oauth_access_token_verify = McCache('OauthAccessTokenVerify.%s') class OauthAccessToken(Model): pass class OauthClient(McModel): txt = txt_property @property def hex_secret(self): return binascii.hexlify(self.secret) def rm(self): if self.cid:
RSS_PO_ID_STATE_NOTAG = 0 RSS_PO_ID_STATE_AUTOTAG = 10 RSS_PO_ID_STATE_TAGED = 20 class RssPoId(Model): pass def rss_po_id_new(user, rss_po_id, po_id): RssPoId.raw_sql( 'insert delayed into rss_po_id '\ '(rss_po_id, po_id, user_id, user_cid, state) '\ 'values (%s, %s, %s, %s, %s)', rss_po_id, po_id, user.id, user.cid, RSS_PO_ID_STATE_NOTAG, ) mc_rss_link_by_po_id = McCache('RssLinkByPoId:%s') @mc_rss_link_by_po_id('{id}') def rss_link_by_po_id(id): rss_po = RssPoId.get(po_id=id) if rss_po: rss_po = RssPo.mc_get(rss_po.rss_po_id) if rss_po: return rss_po.link mc_rss_name = McCache("RssName:%s") @mc_rss_name("{id}") def rss_name_by_rss_id(id): rss = Rss.mc_get(id) if rss: