Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Added book ID caching #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions booklist/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"time"
)

type cachedID struct {
id string
value string
}

type Book struct {
Hash string
FilePath string
Expand All @@ -21,18 +26,30 @@ type Book struct {
Series string
SeriesIndex float64
Publisher string

seriesid cachedID
authorid cachedID
}

func (b *Book) ID() string {
return b.Hash[:10]
}

func (b *Book) cachedIDStr(cached *cachedID, value string) string {
if len(cached.id)==0 || value != cached.value {
cached.value = value
cached.id = fmt.Sprintf("%x", sha1.Sum([]byte(value)))[:10]
}

return cached.id
}

func (b *Book) AuthorID() string {
return fmt.Sprintf("%x", sha1.Sum([]byte(b.Author)))[:10]
return b.cachedIDStr(&b.authorid,b.Author)
}

func (b *Book) SeriesID() string {
return fmt.Sprintf("%x", sha1.Sum([]byte(b.Series)))[:10]
return b.cachedIDStr(&b.seriesid,b.Series)
}

func (b *Book) FileType() string {
Expand Down