Skip to content

Commit

Permalink
add CacheFile
Browse files Browse the repository at this point in the history
  • Loading branch information
malmans2 committed Oct 29, 2024
1 parent 0026014 commit 34dbbf2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cacholote/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@

Base = sa.orm.declarative_base()

association_table = sa.Table(
"association_table",
Base.metadata,
sa.Column("left_id", sa.ForeignKey("cache_entries.id"), primary_key=True),
sa.Column("right_id", sa.ForeignKey("cache_files.id"), primary_key=True),
)


class CacheEntry(Base):
__tablename__ = "cache_entries"
Expand All @@ -48,6 +55,9 @@ class CacheEntry(Base):
updated_at = sa.Column(sa.DateTime, default=utils.utcnow, onupdate=utils.utcnow)
counter = sa.Column(sa.Integer)
tag = sa.Column(sa.String)
cache_files: sa.orm.Mapped[list[CacheFile]] = sa.orm.relationship(
secondary=association_table, back_populates="cache_entries"
)

@property
def _result_as_string(self) -> str:
Expand All @@ -69,6 +79,15 @@ def __repr__(self) -> str:
return f"CacheEntry({public_attrs_repr})"


class CacheFile(Base):
__tablename__ = "cache_files"

id = sa.Column(sa.Integer(), primary_key=True)
cache_entries: sa.orm.Mapped[list[CacheEntry]] = sa.orm.relationship(
secondary=association_table, back_populates="cache_files"
)


@sa.event.listens_for(CacheEntry, "before_insert")
def set_expiration_to_max(
mapper: sa.orm.Mapper[CacheEntry],
Expand Down

0 comments on commit 34dbbf2

Please sign in to comment.