Esempio n. 1
0
    def AddItemCache(itemsCache, forceOverWrite):

        if (itemsCache is None or not (itemsCache is ItemCache)):
            raise errors.ArgumentError("Parameter is invalid (itemsCache)")

        return itemsCache.Save(forceOverWrite)
        pass
Esempio n. 2
0
def _ConvertObjToRedisValue(value):
    if (value is None):
        raise errors.ArgumentError("Parameter is invalid (value)")

    result = value
    statusItem = _StatusItem_None
    if ((type(value) is int) or (type(value) is float) or (type(value) is long)
            or (type(value) is complex) or (type(value) is bool)):
        pass
    elif ((type(value) is str) or (type(value) is unicode)):
        if (config.UseCompression):
            result = _Deflate(result, _COMPRESS)
            statusItem = _StatusItem_Compressed
        else:
            pass
    else:
        if (config.UseCompression):
            result = _Serialize(result, _SERIALIZE)
            result = _Deflate(result, _COMPRESS)
            statusItem = _StatusItem_Compressed + _StatusItem_Serialized
        else:
            result = _Serialize(result, _SERIALIZE)
            statusItem = _StatusItem_Serialized

    return result, statusItem
Esempio n. 3
0
    def GetItemCache(self, key):
        if (key is None or key.strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key)")

        _dal = dal._RedisDal()
        result = _dal.GetListItem(key)

        if (result != None and len(result) > 0):
            ttl_Dt = utilities._TTL_DT_DeSerialize(result[0])
            ttl_Ts = utilities._TTL_TS_DeSerialize(result[0])

            if (utilities._TTL_Is_Expired(ttl_Dt)):
                _dal.DeleteTTL(key)
                return None
            else:
                # Update SLI TTL on Redis...
                if (ttl_Dt[0] != datetime.datetime.max):
                    ttl = utilities._TTLSerialize(ttl_Ts[0], ttl_Ts[1],
                                                  ttl_Dt[1])
                    _dal.UpdateTTL_Item(key, ttl)
                    _dal.SetTTL(key, ttl_Ts[0])
                else:
                    pass

                ic = ItemCache()
                ic.SlidingExpiration = ttl_Ts[0]
                ic.AbsoluteExpiration = ttl_Ts[1]
                ic.Key = key
                ic.Value = result[1]
                return ic
        else:
            return None

        pass
Esempio n. 4
0
    def GetValue(self, key):
        if (key is None or key.strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key)")

        result = self.GetItemCache(key)
        if (result != None):
            return result.Value
        else:
            return None
        pass
Esempio n. 5
0
    def ItemExist(self, key):
        if (key is None or str(key).strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key)")

        try:
            result = self._db.exists(key)
            return result
        except (Exception):
            raise
        finally:
            pass

        pass
Esempio n. 6
0
    def SetTTL(self, key, ttl):
        if (key is None or str(key).strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key)")

        try:
            result = self._db.expire(key, ttl)
            return result
        except (Exception):
            raise
        finally:
            pass

        pass
Esempio n. 7
0
def _TTL_Is_Expired(ttl):

    if (ttl is None):
        raise errors.ArgumentError("Parameter is invalid (ttl)")

    dtNow = datetime.datetime.utcnow()
    dt = ttl
    if (type(ttl) is str):
        dt = _TTL_DT_DeSerialize(ttl)

    if (dtNow > dt[0]) or (dtNow > dt[1]):
        return True
    else:
        return False
Esempio n. 8
0
    def UpdateTTL_Item(self, key, ttl):
        if (key is None or str(key).strip() == "" or ttl is None
                or str(ttl).strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key or value)")

        try:
            result = self._db.lset(key, 0, ttl)
            return result
        except (Exception):
            raise
        finally:
            pass

        pass
Esempio n. 9
0
    def AddListItem(self, key, value):
        if (key is None or str(key).strip() == "" or value is None
                or str(value).strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key or value)")

        try:
            #value = utilities._ConvertObjToRedisValue(value)    #TODO
            result = self._db.rpush(key, value)
            return result
        except (Exception):
            raise
        finally:
            pass

        pass
Esempio n. 10
0
def _ConvertRedisValueToObject(value, statusItem):
    if (value is None or statusItem is None):
        raise errors.ArgumentError(
            "Parameter is invalid (value or statusItem)")

    result = value
    if ((statusItem & _StatusItem_None) == _StatusItem_None):
        pass
    if ((statusItem & _StatusItem_Compressed) == _StatusItem_Compressed):
        result = _Deflate(result, _DECOMPRESS)
        pass
    if ((statusItem & _StatusItem_Serialized) == _StatusItem_Serialized):
        result = _Serialize(result, _DESERIALIZE)
        pass

    return result
Esempio n. 11
0
    def AddListItemWithTTL(self, key, value, value_ttl):
        if (key is None or str(key).strip() == "" or value is None
                or str(value).strip() == "" or value_ttl is None
                or str(value_ttl).strip() == ""):
            raise errors.ArgumentError(
                "Parameter is invalid (key or value or value_ttl)")

        try:
            result = self._db.rpush(key, value_ttl)
            result = self._db.rpush(key, value)
            return result
        except (Exception):
            raise
        finally:
            pass

        pass
Esempio n. 12
0
    def GetListItem(self, key):
        if (key is None or str(key).strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key)")

        try:
            val = self._db.lrange(key, 0, 1)
            if (val is None or len(val) == 0):
                result = None
            else:
                #val = utilities._ConvertRedisValueToObject(value, t)     #TODO
                result = (val[0], val[1])
            return result
        except (Exception):
            raise
        finally:
            pass

        pass
Esempio n. 13
0
def _TTL_DT_DeSerialize(ttl):

    if (ttl is None or ttl.strip() == ""):
        raise errors.ArgumentError("Parameter is invalid (ttl)")

    dts = ttl.split("|")
    dtSLI = datetime.datetime.max
    dtABS = datetime.datetime.max

    if (dts[0] != _No_TTL):
        dtSLI = datetime.datetime.strptime(dts[0], "%Y%m%dT%H%M%S")
        pass

    if (dts[2] != _No_TTL):
        dtABS = datetime.datetime.strptime(dts[2], "%Y%m%dT%H%M%S")
        pass

    result = (dtSLI, dtABS)
    return result
Esempio n. 14
0
def _TTL_TS_DeSerialize(ttl):

    if (ttl is None or ttl.strip() == ""):
        raise errors.ArgumentError("Parameter is invalid (ttl)")

    dts = ttl.split("|")
    tsSLI = _NO_EXPIRATION
    tsABS = _NO_EXPIRATION

    if (dts[1] != _No_TTL):
        tsSLI = datetime.timedelta(seconds=int(dts[1]))
        pass

    if (dts[3] != _No_TTL):
        tsABS = datetime.timedelta(seconds=int(dts[3]))
        pass

    result = (tsSLI, tsABS)
    return result
Esempio n. 15
0
    def Add(self, key, value, slidingExpiration, absoluteExpiration,
            forceOverWrite):
        if (key is None or key.strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key)")

        if (slidingExpiration != utilities._NO_EXPIRATION
                and absoluteExpiration != utilities._NO_EXPIRATION) and (
                    slidingExpiration >= absoluteExpiration):
            raise errors.GenericError(
                "Sliding Expiration is greater or equal than Absolute Expiration."
            )
            pass

        _dal = dal._RedisDal()
        ttl = utilities._TTLSerialize(slidingExpiration, absoluteExpiration,
                                      datetime.datetime.max)
        if (self._TypeStorage == config.UseList):
            if (not forceOverWrite):
                if (_dal.ItemExist(key)):
                    raise errors.GenericError("This Item Exists.")
                    pass
                else:
                    # Continue...
                    pass
                pass
            else:
                if (_dal.ItemExist(key)):
                    _dal.ItemDelete(key)
                    pass
                else:
                    # Continue...
                    pass
                pass
            pass

            result = _dal.AddListItemWithTTL(key, value, ttl)
            self._SetTTL(key, slidingExpiration, absoluteExpiration, _dal)
            return result
        else:
            raise errors.GenericError("NotImplementedException.")
            pass
        pass
Esempio n. 16
0
    def Delete(self, key):
        if (key is None or key.strip() == ""):
            raise errors.ArgumentError("Parameter is invalid (key)")

        _dal = dal._RedisDal()
        return _dal.DeleteTTL(key)