Esempio n. 1
0
 def AddListItem_Test(self):
     dal._RedisDal().ItemDelete("key1234")
     result = dal._RedisDal().AddListItem("key1234", "val1")
     result = dal._RedisDal().AddListItem("key1234", "val2")
     result = dal._RedisDal().GetListItem("key1234")
     self.assertTupleEqual(result, ('val1', 'val2'))         
     print "AddListItem_Test::OK"
     pass
Esempio n. 2
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. 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 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. 5
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. 6
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)
Esempio n. 7
0
 def AddListItemWithTTL_Test(self):
     
     #Set TTL
     ttlSLI = datetime.timedelta(hours=0, minutes=0, seconds=20)
     ttlABS = datetime.timedelta(hours=0, minutes=0, seconds=30)
     ttl = utilities._TTLSerialize(ttlSLI, ttlABS, datetime.datetime.max)
     
     dal._RedisDal().ItemDelete("key456")
     
     result = dal._RedisDal().AddListItemWithTTL("key456", "value_1", ttl)
     
     dal._RedisDal().SetTTL("key456", ttlSLI)
     
     result = dal._RedisDal().GetListItem("key456")
     
     self.assertTupleEqual(result, (ttl, 'value_1'))
     self.assertTrue(dal._RedisDal()._db.ttl("key456"), 20)
     
     
     #Update TTL
     ttlSLI = datetime.timedelta(hours=0, minutes=0, seconds=10)
     ttl = utilities._TTLSerialize(ttlSLI, ttlABS, datetime.datetime.max)
     
     dal._RedisDal().SetTTL("key456", ttlSLI)
     dal._RedisDal().UpdateTTL_Item("key456", ttl)
     result = dal._RedisDal().GetListItem("key456")
     
     self.assertTrue(dal._RedisDal()._db.ttl("key456"), 10)
     self.assertTupleEqual(result, (ttl, 'value_1'))
     
     #Delete TTL
     ttlSLI = config.DefaultSlidingExpiration
     ttlABS = config.DefaultAbsoluteExpiration
     ttl = utilities._TTLSerialize(ttlSLI, ttlABS, datetime.datetime.max)
     
     dal._RedisDal().UpdateTTL_Item("key456", ttl)
     dal._RedisDal().DeleteTTL("key456")
     
     result = dal._RedisDal().GetListItem("key456")
     
     self.assertTrue(dal._RedisDal()._db.ttl("key456"), -1)
     self.assertTupleEqual(result, (ttl, 'value_1'))
     
     print "AddListItemWithTTL_Test::OK"
     pass
Esempio n. 8
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)