Exemplo n.º 1
0
    def new(self, data, isSyncNow=False):
        """创建一个新的对象
        """
        incrkey = self._incrkey
        if incrkey:
            incrvalue = self.incr('_incrvalue', 1)
            data[incrkey] = incrvalue - 1
            pk = data.get(self._pk)
            if pk is None:
                raise PKValueError(data)
            mm = MMode(self._name + ':%s' % pk, self._pk, data=data)
            setattr(mm, incrkey, pk)
        else:
            pk = data.get(self._pk)
            mm = MMode(self._name + ':%s' % pk, self._pk, data=data)
        if self._fk:
            fk = data.get(self._fk, 0)
            name = '%s_fk:%s' % (self._name, fk)
            fkmm = MFKMode(name)
            pklist = fkmm.get('pklist')
            if pklist is None:
                pklist = self.getAllPkByFk(fk)
            if pk not in pklist:
                pklist.append(pk)
            fkmm.update('pklist', pklist)
        setattr(mm, '_state', MMODE_STATE_NEW)
        mm.insert()

        if isSyncNow:
            mm.syncDB()  # 即时同步到数据库
        else:
            rdsclient.zadd(mm.dszset, mm.produceRedisKey(pk), time.time())

        return mm
Exemplo n.º 2
0
    def update_multi(self,
                     mapping,
                     isSyncNow=False):  # 如果改用redis将不允许非全字段更新,必须全表更新。
        if len(mapping) == 0:
            return
        ntime = time.time()
        data = self.get_multi(['data', '_state'])
        data['data'].update(mapping)
        if data.get('_state') == MMODE_STATE_NEW:
            props = {'data': data.get('data'), '_time': ntime}
            result = MemObject.update_multi(self, props)
            if isSyncNow:
                self.syncDB()
        else:
            props = {
                '_state': MMODE_STATE_UPDATE,
                'data': data.get('data'),
                '_time': ntime
            }
            result = MemObject.update_multi(self, props)
            if isSyncNow:
                self.syncDB()

        if not isSyncNow:
            pkval = data.get('data').get(self._pk)
            rdsclient.zadd(self.dszset, self.produceRedisKey(pkval), ntime)
        return result
Exemplo n.º 3
0
 def delete(self, isSyncNow=True):
     '''删除对象
     '''
     if isSyncNow:
         MemObject.update(self, '_state', MMODE_STATE_DEL)  # add by fage
         self.syncDB()
     else:
         MemObject.update(self, '_state', MMODE_STATE_DEL)
         pkval = self.get('data').get(self._pk)
         rdsclient.zadd(self.dszset, self.produceRedisKey(pkval), time.time())
Exemplo n.º 4
0
    def update(self, key, values):
        data = self.get_multi(['data', '_state'])
        ntime = time.time()
        data['data'].update({key: values})
        if data.get('_state') == MMODE_STATE_NEW:
            props = {'data': data.get('data'), '_time': ntime}
        else:
            props = {'_state': MMODE_STATE_UPDATE, 'data': data.get('data'), '_time': ntime}

        pkval = data.get('data').get(self._pk)
        rdsclient.zadd(self.dszset, self.produceRedisKey(pkval), ntime)
        return MemObject.update_multi(self, props)