Skip to content

Commit

Permalink
[cache/fs] New sub-package
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jul 30, 2024
1 parent ce21b06 commit 7c0cf62
Show file tree
Hide file tree
Showing 6 changed files with 627 additions and 0 deletions.
1 change: 1 addition & 0 deletions .scripts/packages.list
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* + ansi
* + cache/memory
* + cache/fs
* + color
* + cron
* + csv
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### [13.3.0](https://kaos.sh/ek/13.3.0)

- `[cache/fs]` Added cache with file system storage
- `[cache]` In-memory cache moved to `cache/memory`
- `[sliceutil]` Added method `Join`

### [13.2.1](https://kaos.sh/ek/13.2.1)
Expand Down
128 changes: 128 additions & 0 deletions cache/fs/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package fs

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"time"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleNew() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")

fmt.Println(cache.Get("test"))
}

func ExampleCache_Set() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")
cache.Set("test", "ABCD", 15*time.Minute)

fmt.Println(cache.Get("test"))
}

func ExampleCache_Has() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")

fmt.Println(cache.Has("test"))
}

func ExampleCache_Get() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")

fmt.Println(cache.Get("test"))
}

func ExampleCache_Size() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")

fmt.Println(cache.Size())
}

func ExampleCache_Expired() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")

fmt.Println(cache.Expired())
}

func ExampleCache_GetWithExpiration() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")

item, exp := cache.GetWithExpiration("test")

fmt.Println(item, exp.String())
}

func ExampleCache_Delete() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")
cache.Delete("test")

fmt.Println(cache.Get("test"))
}

func ExampleCache_Flush() {
cache, _ := New(Config{
Dir: "/path/to/cache",
DefaultExpiration: time.Minute,
CleanupInterval: time.Minute,
})

cache.Set("test", "ABCD")
cache.Flush()

fmt.Println(cache.Get("test"))
}
Loading

0 comments on commit 7c0cf62

Please sign in to comment.