From 8ee031a9e82d96fa15fe4d1cf32479da39ce85bf Mon Sep 17 00:00:00 2001 From: Alexander Neonxp Kiryukhin Date: Sat, 14 Mar 2026 21:13:39 +0300 Subject: =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B4=D0=BE=D1=81=D0=B0=D0=B4=D0=BD=D1=8B=D0=B9=20=D0=B1=D0=B0?= =?UTF-8?q?=D0=B3=20=D1=81=20=D0=BF=D0=BE=D0=B2=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D1=8B=D0=BC=20=D0=BF=D0=BE=D1=81=D1=82=D0=B8=D0=BD=D0=B3=D0=BE?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/database/db.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/database/db.go b/internal/database/db.go index 2fbfe74..d5aa1f4 100644 --- a/internal/database/db.go +++ b/internal/database/db.go @@ -11,11 +11,13 @@ import ( "bytes" "fmt" "os" + "sync" ) type DB struct { fp *os.File cache map[string]struct{} + mu *sync.RWMutex } func New(path string) (*DB, error) { @@ -28,6 +30,7 @@ func New(path string) (*DB, error) { db := &DB{ fp: fp, cache: map[string]struct{}{}, + mu: &sync.RWMutex{}, } for part := range bytes.SplitSeq(cache, []byte("\n")) { db.cache[string(part)] = struct{}{} @@ -41,14 +44,22 @@ func (d *DB) Close() error { } func (d *DB) Exists(s string) bool { + d.mu.RLock() + defer d.mu.RUnlock() + _, ok := d.cache[s] return ok } func (d *DB) Append(s string) error { + d.mu.Lock() + defer d.mu.Unlock() + if d.Exists(s) { return nil } + + d.cache[s] = struct{}{} _, err := fmt.Fprintln(d.fp, s) return err } -- cgit v1.2.3