def new(cls, id, url, name): if url == str(id): url = '' if url.isdigit(): id = int(url) url = '' o = None if id: o = cls.mc_get(id) if o is None and url: o = cls.get(url=url) if o is None: o = cls() o.id = id if url: o.url = url #print url key = mc_model_url%(cls.__name__ , url) mc.set(key, id) key = mc_model_url%(cls.__name__ , id) mc.set(key, id) if name: o.name = name o.save() return o
def mc_id_by_value_new(self, value): id = self.mc_id_by_value(value) if not id: id = self.insert(value) h = md5(value).hexdigest() mc_key = self.__mc_value__ % h mc.set(mc_key, id) return id
def mc_id_by_value(self, value): h = md5(value).hexdigest() mc_key = self.__mc_value__ % h r = mc.get(mc_key) if r is None: r = self.id_by_value(value) mc.set(mc_key, r) return r
def insert_no_value_cache(self, value): cursor = self.cursor cursor.execute( 'insert into %s (value) values (%%s)' % self.__table__, value ) cursor.connection.commit() id = cursor.lastrowid mc_key = self.__mc_id__ % id mc.set(mc_key, value) return id
def get(self, id): mc_key = self.__mc_id__ % id r = mc.get(mc_key) if r is None: cursor = self.cursor cursor.execute('select value from %s where id=%%s' % self.__table__, id) r = cursor.fetchone() if r: r = r[0] if r is None: r = self.NULL mc.set(mc_key, r) return r
def by_url(cls, url): key = mc_model_url%(cls.__name__ , url) id = mc.get(key) if id is None: if type(url) in (int, long) or url.isdigit(): o = cls.mc_get(url) if o: id = o.id else: o = cls.get(url=url) if o: id = o.id mc.set(key, id or 0) return id
def set(self, id, value): r = self.get(id) if r != value: mc_key = self.__mc_id__ % id cursor = self.cursor table = self.__table__ cursor.execute( 'insert into %s (id,value) values (%%s,%%s) on duplicate key update value=values(value)' % table, (id, value) ) cursor.connection.commit() if value is None: value = False mc.set(mc_key, value)
def mc_value_id_set(self, value, id): h = md5(value).hexdigest() mc_key = self.__mc_value__ % h mc.set(mc_key, id)
setattr(i, key, func(i.id)) def ico_url_bind(zsite_list): return ico_url_bind_with_default(zsite_list, ico_url) def pic_url_bind(zsite_list, size): key = "pic%s" % size for i in zsite_list: setattr(i, key, pic_url(i.id, size)) def pic_url_bind_with_default(zsite_list, size): key = "pic%s" % size for i in zsite_list: setattr(i, key, pic_url_with_default(i.id, size)) if __name__ == "__main__": # print ico_url(399) # print pic_url(399) from _db import mc mc = mc.mc for i in range(100000): mc.set("1", 1) mc.delete("1") if mc.get("1"): print "!!!"
def pic_url_bind(zsite_list, size): key = 'pic%s' % size for i in zsite_list: setattr( i, key, pic_url(i.id, size) ) def pic_url_bind_with_default(zsite_list, size): key = 'pic%s' % size for i in zsite_list: setattr( i, key, pic_url_with_default(i.id, size) ) if __name__ == '__main__': #print ico_url(399) #print pic_url(399) from _db import mc mc = mc.mc for i in range(100000): mc.set('1', 1) mc.delete('1') if mc.get('1'): print '!!!'