Skip to content

Json store with atomic.Value copy on write and context closing

License

Notifications You must be signed in to change notification settings

covrom/jsonstore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonstore

Json store with atomic.Value copy on write and context closing

Usage:

type Config struct{
    A,B string
}

func (cf *Config) Clone() jsonstore.Value{
    return &Config{
        A: cf.A,
        B: cf.B,
    }
}

tmpfn := "/tmp/testJsonSyncStore.json"

str := &Config{"aaa", "bbb"}
store, err := NewJsonSyncStore(context.Background(), "/tmp/testJsonSyncStore.json", str, time.NewTicker(time.Second), false)
if err != nil {
    fmt.Printf("NewJsonSyncStore() error = %v", err)
    return
}

cnt := int64(0)

f := func(d <-chan struct{}, st Store) {
    tick := time.NewTicker(100 * time.Millisecond)
    defer tick.Stop()
    for {
        select {
        case <-d:
            return
        case t := <-tick.C:
            c := atomic.AddInt64(&cnt, 1)
            
            set := st.Load().(*Config)
            set.A = t.String()
            set.B = fmt.Sprintf("changed by iteration %d", c)
            st.Store(set)
        }
    }
}

store.Go(f)
store.Go(f)
store.Go(f)

time.AfterFunc(2*time.Second, func() {
    fmt.Println(time.Now().String())
    store.Close()
})

<-time.After(3 * time.Second)

b, err := ioutil.ReadFile(tmpfn)
if err != nil {
    fmt.Printf("NewJsonSyncStore() error = %v", err)
    return
}
fmt.Println(string(b))

About

Json store with atomic.Value copy on write and context closing

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages