Skip to content

Commit

Permalink
fix(db): account wrapping object in size calculation for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eklmv committed Jan 21, 2024
1 parent baf6f7e commit b25af28
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
7 changes: 4 additions & 3 deletions internal/db/cachedqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ func NewCachedQueries(cache cache.Cache[uint32, cachedResponse], querier Querier

func (cq *CachedQueries) addToCache(p prefix, str string, value any) {
hash := cache.HashString(p.String() + str)
cq.c.Add(hash, cachedResponse{
r := cachedResponse{
value: value,
size: cache.SizeOf(value),
})
}
r.size = cache.SizeOf(r)
cq.c.Add(hash, r)
}

func (cq *CachedQueries) invalidateCache(p prefix, str string) {
Expand Down
43 changes: 21 additions & 22 deletions internal/db/cachedqueries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strconv"
"testing"
"time"
"unsafe"

"github.com/eklmv/pdfcertificates/internal/cache"
"github.com/jackc/pgx/v5/pgtype"
Expand Down Expand Up @@ -53,7 +52,7 @@ func TestCachedQueriesCreateCertificate(t *testing.T) {
hash := cache.HashString(prefCert.String() + exp.CertificateID)
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.CertificateID)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand All @@ -76,7 +75,7 @@ func TestCachedQueriesCreateCourse(t *testing.T) {
hash := cache.HashString(prefCourse.String() + strconv.Itoa(int(exp.CourseID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Data)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand All @@ -99,7 +98,7 @@ func TestCachedQueriesCreateStudent(t *testing.T) {
hash := cache.HashString(prefStudent.String() + strconv.Itoa(int(exp.StudentID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Data)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand All @@ -122,7 +121,7 @@ func TestCachedQueriesCreateTemplate(t *testing.T) {
hash := cache.HashString(prefTmpl.String() + strconv.Itoa(int(exp.TemplateID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Content)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand Down Expand Up @@ -180,7 +179,7 @@ func TestCachedQueriesDeleteCertificate(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(cert))+uint64(len(cert.CertificateID)), c.Size())
assert.Equal(t, cert, c.Values()[0].value)
m.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -223,7 +222,7 @@ func TestCachedQueriesDeleteCourse(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(course))+uint64(len(course.Data)), c.Size())
assert.Equal(t, course, c.Values()[0].value)
m.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -306,8 +305,8 @@ func TestCachedQueriesDeleteStudent(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(2), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(student)+unsafe.Sizeof(cert))+
uint64(len(student.Data)+len(cert.CertificateID)), c.Size())
assert.Equal(t, student, c.Values()[0].value)
assert.Equal(t, cert, c.Values()[1].value)
m.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -350,7 +349,7 @@ func TestCachedQueriesDeleteTemplate(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(tmpl))+uint64(len(tmpl.Content)), c.Size())
assert.Equal(t, tmpl, c.Values()[0].value)
m.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -407,7 +406,7 @@ func TestCachedQueriesGetCertificate(t *testing.T) {
hash := cache.HashString(prefCert.String() + exp.CertificateID)
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.CertificateID)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand Down Expand Up @@ -447,7 +446,7 @@ func TestCachedQueriesGetCourse(t *testing.T) {
hash := cache.HashString(prefCourse.String() + strconv.Itoa(int(exp.CourseID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Data)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand Down Expand Up @@ -487,7 +486,7 @@ func TestCachedQueriesGetStudent(t *testing.T) {
hash := cache.HashString(prefStudent.String() + strconv.Itoa(int(exp.StudentID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Data)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand Down Expand Up @@ -527,7 +526,7 @@ func TestCachedQueriesGetTemplate(t *testing.T) {
hash := cache.HashString(prefTmpl.String() + strconv.Itoa(int(exp.TemplateID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Content)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
}

Expand Down Expand Up @@ -558,7 +557,7 @@ func TestCachedQueriesUpdateCertificate(t *testing.T) {
hash := cache.HashString(prefCert.String() + exp.CertificateID)
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.CertificateID)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
t.Run("if certificate update failed cache should be intact", func(t *testing.T) {
cq, c, m := prepCachedQueries(t)
Expand Down Expand Up @@ -591,7 +590,7 @@ func TestCachedQueriesUpdateCertificate(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(cert))+uint64(len(cert.CertificateID)), c.Size())
assert.Equal(t, cert, c.Values()[0].value)
m.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -637,7 +636,7 @@ func TestCachedQueriesUpdateCourse(t *testing.T) {
hash := cache.HashString(prefCourse.String() + strconv.Itoa(int(exp.CourseID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Data)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
t.Run("if course update failed cache should be intact", func(t *testing.T) {
cq, c, m := prepCachedQueries(t)
Expand Down Expand Up @@ -670,7 +669,7 @@ func TestCachedQueriesUpdateCourse(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(cert))+uint64(len(cert.CertificateID)), c.Size())
assert.Equal(t, cert, c.Values()[0].value)
m.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -716,7 +715,7 @@ func TestCachedQueriesUpdateStudent(t *testing.T) {
hash := cache.HashString(prefStudent.String() + strconv.Itoa(int(exp.StudentID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Data)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
t.Run("if student update failed cache should be intact", func(t *testing.T) {
cq, c, m := prepCachedQueries(t)
Expand Down Expand Up @@ -749,7 +748,7 @@ func TestCachedQueriesUpdateStudent(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(cert))+uint64(len(cert.CertificateID)), c.Size())
assert.Equal(t, cert, c.Values()[0].value)
m.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -795,7 +794,7 @@ func TestCachedQueriesUpdateTemplate(t *testing.T) {
hash := cache.HashString(prefTmpl.String() + strconv.Itoa(int(exp.TemplateID)))
assert.Contains(t, c.Keys(), hash)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(exp))+uint64(len(exp.Content)), c.Size())
assert.Equal(t, exp, c.Values()[0].value)
})
t.Run("if student update failed cache should be intact", func(t *testing.T) {
cq, c, m := prepCachedQueries(t)
Expand Down Expand Up @@ -828,7 +827,7 @@ func TestCachedQueriesUpdateTemplate(t *testing.T) {
assert.ErrorContains(t, err, "failed")
assert.Empty(t, got)
assert.Equal(t, uint64(1), c.Len())
assert.Equal(t, uint64(unsafe.Sizeof(cert))+uint64(len(cert.CertificateID)), c.Size())
assert.Equal(t, cert, c.Values()[0].value)
m.AssertExpectations(t)
})
}

0 comments on commit b25af28

Please sign in to comment.