Adds a database.
This is a really simple map. Calls to it just check if the key exists and return a bool. In case of a false it adds the key. The key is just a string which might or might not be sufficient.
This commit is contained in:
parent
bfbe2dd3be
commit
90c93b8fa4
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
Credits go to github.com/SlyMarbo/rss for inspiring this solution.
|
||||
*/
|
||||
package feeder
|
||||
|
||||
type database struct {
|
||||
request chan string
|
||||
response chan bool
|
||||
known map[string]struct{}
|
||||
}
|
||||
|
||||
func (d *database) Run() {
|
||||
d.known = make(map[string]struct{})
|
||||
var s string
|
||||
|
||||
for {
|
||||
s = <-d.request
|
||||
if _, ok := d.known[s]; ok {
|
||||
d.response <- true
|
||||
} else {
|
||||
d.response <- false
|
||||
d.known[s] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewDatabase() *database {
|
||||
database := new(database)
|
||||
database.request = make(chan string)
|
||||
database.response = make(chan bool)
|
||||
go database.Run()
|
||||
return database
|
||||
}
|
Loading…
Reference in New Issue