Beispiel #1
0
    def _incr(self, prop, amt = 1):

        if self._dirty:
            raise ValueError, "cannot incr dirty thing"

        prefix = thing_prefix(self.__class__.__name__)
        key =  prefix + prop + '_' + str(self._id)
        cache_val = old_val = cache.get(key)
        if old_val is None:
            old_val = getattr(self, prop)

        if self._defaults.has_key(prop) and self._defaults[prop] == old_val:
            #potential race condition if the same property gets incr'd
            #from default at the same time
            setattr(self, prop, old_val + amt)
            self._commit(prop)
        else:
            self.__setattr__(prop, old_val + amt, False)
            #db
            if prop.startswith('_'):
                tdb.incr_thing_prop(self._type_id, self._id, prop[1:], amt)
            else:
                self._incr_data(self._type_id, self._id, prop, amt)
            cache.set(prefix + str(self._id), self)
            
        #cache
        if cache_val:
            cache.incr(key, amt)
        else:
            cache.set(key, getattr(self, prop))
Beispiel #2
0
    def _incr(self, prop, amt = 1):
        if self._dirty:
            raise ValueError, "cannot incr dirty thing"

        #make sure we're incr'ing an _int_prop or _data_int_prop.
        if prop not in self._int_props:
            if (prop in self._data_int_props or
                self._int_prop_suffix and prop.endswith(self._int_prop_suffix)):
                #if we're incr'ing a data_prop, make sure we're loaded
                if not self._loaded:
                    self._load()
            else:
                msg = ("cannot incr non int prop %r on %r -- it's not in %r or %r" %
                       (prop, self, self._int_props, self._data_int_props))
                raise ValueError, msg

        with g.make_lock("thing_commit", 'commit_' + self._fullname):
            self._sync_latest()
            old_val = getattr(self, prop)
            if self._defaults.has_key(prop) and self._defaults[prop] == old_val:
                #potential race condition if the same property gets incr'd
                #from default at the same time
                setattr(self, prop, old_val + amt)
                self._commit(prop)
            else:
                self.__setattr__(prop, old_val + amt, False)
                #db
                if prop.startswith('_'):
                    tdb.incr_thing_prop(self._type_id, self._id, prop[1:], amt)
                else:
                    self._incr_data(self._type_id, self._id, prop, amt)

            self._cache_myself()
Beispiel #3
0
    def _incr(self, prop, amt = 1):

        if self._dirty:
            raise ValueError, "cannot incr dirty thing"

        prefix = thing_prefix(self.__class__.__name__)
        key =  prefix + prop + '_' + str(self._id)
        cache_val = old_val = cache.get(key)
        if old_val is None:
            old_val = getattr(self, prop)

        if self._defaults.has_key(prop) and self._defaults[prop] == old_val:
            #potential race condition if the same property gets incr'd
            #from default at the same time
            setattr(self, prop, old_val + amt)
            self._commit(prop)
        else:
            self.__setattr__(prop, old_val + amt, False)
            #db
            if prop.startswith('_'):
                tdb.incr_thing_prop(self._type_id, self._id, prop[1:], amt)
            else:
                self._incr_data(self._type_id, self._id, prop, amt)
            cache.set(prefix + str(self._id), self)
            
        #cache
        if cache_val:
            cache.incr(key, amt)
        else:
            cache.set(key, getattr(self, prop))