@@ -835,15 +835,17 @@ def __init__(self,title,fname,maxsize=0,Timeout=180,Log=None):
835835 def read (self ):
836836 dataDB = None
837837
838- self .fw .Lock ()
839- try :
840- data = ReadFile (self .fname )
841- dataDB = json .loads (data )
842- except :
843- pass
844- self .fw .Unlock ()
838+ l = self .fw .Lock ()
839+ if l == 'locked' :
840+ try :
841+ data = ReadFile (self .fname )
842+ dataDB = json .loads (data )
843+ except :
844+ pass
845+ self .fw .Unlock ()
845846
846- return dataDB
847+ return dataDB
848+ return False
847849
848850 # Return a count that does NOT include expired items
849851
@@ -861,62 +863,66 @@ def update(self,key,payload,expire):
861863 dataDB = {}
862864 results = {}
863865
864- self .fw .Lock ()
865- try :
866- data = ReadFile (self .fname )
867-
868- if data != None and data != '' :
869- dataDB = json .loads (data )
870- if dataDB != None :
871- if key in dataDB :
872- dataItem = json .loads (dataDB [key ])
873- if dataItem ['Expire' ]> time .time ():
874- # Found and not expired, return result
875- if expire == 0 :
876- # Force kill item
877- dataItem ['Expire' ]= expire
878- dataDB [key ]= json .dumps (dataItem )
879- results ['Status' ]= 'Expired'
880- results ['Payload' ]= dataItem
881- else :
882- results ['Status' ]= 'Found'
883- results ['Payload' ]= dataItem
884- else : # Found and expired, replace old data with new data
866+ l = self .fw .Lock ()
867+ if l == 'locked' :
868+ try :
869+ data = ReadFile (self .fname )
870+
871+ if data != None and data != '' :
872+ dataDB = json .loads (data )
873+ if dataDB != None :
874+ if key in dataDB :
875+ dataItem = json .loads (dataDB [key ])
876+ if dataItem ['Expire' ]> time .time ():
877+ # Found and not expired, return result
878+ if expire == 0 :
879+ # Force kill item
880+ dataItem ['Expire' ]= expire
881+ dataDB [key ]= json .dumps (dataItem )
882+ results ['Status' ]= 'Expired'
883+ results ['Payload' ]= dataItem
884+ else :
885+ results ['Status' ]= 'Found'
886+ results ['Payload' ]= dataItem
887+ else : # Found and expired, replace old data with new data
888+ c = self .countDB (dataDB )
889+ if (self .maxsize == 0 ) or (self .maxsize > 0 and c < self .maxsize ):
890+ dataItem ['Expire' ]= time .time ()+ expire
891+ dataItem ['Payload' ]= payload
892+ dataDB [key ]= json .dumps (dataItem )
893+ results ['Status' ]= 'Replaced'
894+ results ['Payload' ]= dataItem
895+ else : # Size limit hit
896+ results ['Status' ]= 'Error'
897+ results ['Payload' ]= 'Maximum size limit exceeded'
898+ else : # New item
899+ # Needs to deal with expired item not being counted in limits
885900 c = self .countDB (dataDB )
886901 if (self .maxsize == 0 ) or (self .maxsize > 0 and c < self .maxsize ):
902+ dataItem = {}
887903 dataItem ['Expire' ]= time .time ()+ expire
888904 dataItem ['Payload' ]= payload
889905 dataDB [key ]= json .dumps (dataItem )
890- results ['Status' ]= 'Replaced '
906+ results ['Status' ]= 'Added '
891907 results ['Payload' ]= dataItem
892908 else : # Size limit hit
893909 results ['Status' ]= 'Error'
894910 results ['Payload' ]= 'Maximum size limit exceeded'
895- else : # New item
896- # Needs to deal with expired item not being counted in limits
897- c = self .countDB (dataDB )
898- if (self .maxsize == 0 ) or (self .maxsize > 0 and c < self .maxsize ):
899- dataItem = {}
900- dataItem ['Expire' ]= time .time ()+ expire
901- dataItem ['Payload' ]= payload
902- dataDB [key ]= json .dumps (dataItem )
903- results ['Status' ]= 'Added'
904- results ['Payload' ]= dataItem
905- else : # Size limit hit
906- results ['Status' ]= 'ErrorLimit'
907- results ['Payload' ]= 'Maximum size limit exceeded'
908- else : # First Item
909- dataItem = {}
910- dataItem ['Expire' ]= time .time ()+ expire
911- dataItem ['Payload' ]= payload
912- dataDB [key ]= json .dumps (dataItem )
913- results ['Status' ]= 'Added'
914- results ['Payload' ]= dataItem
915-
916- WriteFile (self .fname ,json .dumps (dataDB ))
917- self .fw .Unlock ()
918- except :
919- self .fw .Unlock ()
911+ else : # First Item
912+ dataItem = {}
913+ dataItem ['Expire' ]= time .time ()+ expire
914+ dataItem ['Payload' ]= payload
915+ dataDB [key ]= json .dumps (dataItem )
916+ results ['Status' ]= 'Added'
917+ results ['Payload' ]= dataItem
918+
919+ WriteFile (self .fname ,json .dumps (dataDB ))
920+ self .fw .Unlock ()
921+ except :
922+ self .fw .Unlock ()
923+ else :
924+ results ['Status' ]= 'Error'
925+ results ['Payload' ]= 'Locked Failed'
920926 return results
921927
922928 # Search for a specific item
@@ -939,22 +945,23 @@ def search(self,key):
939945
940946 def purge (self ):
941947 dataDB = {}
942- self .fw .Lock ()
943- try :
944- data = ReadFile (self .fname )
945- if data != None and data != '' :
946- dataDB = json .loads (data )
947-
948- # Remove expired entries
949- NewDataDB = {}
950- for cur in dataDB :
951- dataItem = json .loads (dataDB [cur ])
952- if dataItem ['Expire' ]> time .time ():
953- NewDataDB [cur ]= dataDB [cur ]
954- WriteFile (self .fname ,json .dumps (NewDataDB ))
955- self .fw .Unlock ()
956- except :
957- self .fw .Unlock ()
948+ l = self .fw .Lock ()
949+ if l == 'locked' :
950+ try :
951+ data = ReadFile (self .fname )
952+ if data != None and data != '' :
953+ dataDB = json .loads (data )
954+
955+ # Remove expired entries
956+ NewDataDB = {}
957+ for cur in dataDB :
958+ dataItem = json .loads (dataDB [cur ])
959+ if dataItem ['Expire' ]> time .time ():
960+ NewDataDB [cur ]= dataDB [cur ]
961+ WriteFile (self .fname ,json .dumps (NewDataDB ))
962+ self .fw .Unlock ()
963+ except :
964+ self .fw .Unlock ()
958965
959966###
960967### End of module
0 commit comments