[docs]defadd(self,item):ifiteminself.items:# Move to the end to mark as recently usedself.items.move_to_end(item)else:self.items[item]=Trueiflen(self.items)>self.capacity:# Remove the least recently used itemself.items.popitem(last=False)
[docs]@overrideasyncdefget(self,blob_id):""" Retrieve a blob from the database, using its hash. Returns None if not found. :param blob_id: sha1 hash of the blob :return: Blob or None """asyncwithself.engine.connect()asconn:result=awaitconn.execute(select(SqlBlob).where(SqlBlob.id==blob_id),)row=result.fetchone()ifrow:returnBlob(id=blob_id,data=row.data,content_type=row.content_type)
[docs]@overrideasyncdefput(self,blob:Blob)->Blob:""" Store a blob in the database. :param blob_id: sha1 hash of the blob :param data: blob data """ifself.seen.exists(blob.id):returnblobasyncwithself.engine.connect()asconn:ifnot(awaitconn.execute(select(select(SqlBlob.id).where(SqlBlob.id==blob.id).exists(),),)).scalar_one():try:awaitconn.execute(insert(SqlBlob).values(id=blob.id,data=blob.data,content_type=blob.content_type),)awaitconn.commit()exceptIntegrityError:pass# already there? that's fine!returnblob
[docs]@overrideasyncdefdelete(self,blob_id):""" Delete a blob from the database. :param blob_id: sha1 hash of the blob """self.seen.remove(blob_id)asyncwithself.engine.connect()asconn:awaitconn.execute(delete(SqlBlob).where(SqlBlob.id==blob_id),)awaitconn.commit()
[docs]@overrideasyncdefexists(self,blob_id:str)->bool:""" Check if a blob exists in the database. :param blob_id: sha1 hash of the blob :return: True if the blob exists, False otherwise """ifself.seen.exists(blob_id):returnTrueasyncwithself.engine.connect()asconn:returnbool((awaitconn.execute(select(select(SqlBlob.id).where(SqlBlob.id==blob_id).exists(),),)).scalar_one())