-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Support distributed cache with olric * feat: Support external distributed cache * feat: Fix lint * feat: Fix docker-compose to be able to build and tests on CI * feat: Fix docker-compose and create directory for olric configuration * feat: Fix docker-compose * feat: Fix * Update the doc * feat: Update plantuml
- Loading branch information
Showing
555 changed files
with
624 additions
and
198,140 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package providers | ||
|
||
import ( | ||
"github.com/buraksezer/olric/client" | ||
"github.com/buraksezer/olric/config" | ||
"github.com/darkweak/souin/cache/keysaver" | ||
t "github.com/darkweak/souin/configurationtypes" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// Olric provider type | ||
type Olric struct { | ||
*client.Client | ||
dm *client.DMap | ||
keySaver *keysaver.ClearKey | ||
} | ||
|
||
// OlricConnectionFactory function create new Olric instance | ||
func OlricConnectionFactory(configuration t.AbstractConfigurationInterface) (*Olric, error) { | ||
var keySaver *keysaver.ClearKey | ||
if configuration.GetAPI().Souin.Enable { | ||
keySaver = keysaver.NewClearKey() | ||
} | ||
|
||
c, err := client.New(&client.Config{ | ||
Servers: []string{configuration.GetDefaultCache().Olric.URL}, | ||
Client: &config.Client{ | ||
DialTimeout: time.Second, | ||
KeepAlive: time.Second, | ||
MaxConn: 10, | ||
}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return &Olric{ | ||
c, | ||
nil, | ||
keySaver, | ||
}, nil | ||
} | ||
|
||
// ListKeys method returns the list of existing keys | ||
func (provider *Olric) ListKeys() []string { | ||
if nil != provider.keySaver { | ||
return provider.keySaver.ListKeys() | ||
} | ||
return []string{} | ||
} | ||
|
||
// Get method returns the populated response if exists, empty response then | ||
func (provider *Olric) Get(key string) []byte { | ||
val2, err := provider.dm.Get(key) | ||
|
||
if err != nil { | ||
return []byte{} | ||
} | ||
|
||
return val2.([]byte) | ||
} | ||
|
||
// Set method will store the response in Redis provider | ||
func (provider *Olric) Set(key string, value []byte, url t.URL, duration time.Duration) { | ||
if duration == 0 { | ||
ttl, _ := strconv.Atoi(url.TTL) | ||
duration = time.Duration(ttl)*time.Second | ||
} | ||
|
||
err := provider.dm.PutEx(key, value, duration) | ||
if err != nil { | ||
panic(err) | ||
} else { | ||
go func() { | ||
if nil != provider.keySaver { | ||
provider.keySaver.AddKey(key) | ||
} | ||
}() | ||
} | ||
} | ||
|
||
// Delete method will delete the response in Redis provider if exists corresponding to key param | ||
func (provider *Olric) Delete(key string) { | ||
go func() { | ||
err := provider.dm.Delete(key) | ||
if err != nil { | ||
panic(err) | ||
} else { | ||
go func() { | ||
if nil != provider.keySaver { | ||
provider.keySaver.DelKey(key, 0) | ||
} | ||
}() | ||
} | ||
}() | ||
} | ||
|
||
// Init method will initialize Olric provider if needed | ||
func (provider *Olric) Init() error { | ||
dm := provider.Client.NewDMap("souin-map") | ||
|
||
provider.dm = dm | ||
return nil | ||
} | ||
|
||
// Reset method will reset or close provider | ||
func (provider *Olric) Reset() { | ||
provider.Client.Close() | ||
} |
Oops, something went wrong.