Skip to content

Commit

Permalink
Added new method DeleteWithoutDependency()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 23, 2019
1 parent a31a28f commit 93ca890
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
21 changes: 21 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ func Delete(keys ...string) (total int, err error) {
return KillByDependency(keys...)
}

// DeleteWithoutDependency will remove keys without using dependency script
func DeleteWithoutDependency(keys ...string) (total int, err error) {

// Create a new connection and defer closing
conn := GetConnection()
defer func() {
_ = conn.Close()
}()

// Loop all keys and delete
for _, key := range keys {
_, err = conn.Do("DEL", key)
if err != nil {
return
}
total++
}

return
}

// HashSet will set the hashKey to the value in the specified hashName and link a
// reference to each dependency for the entire hash
func HashSet(hashName, hashKey string, value interface{}, dependencies ...string) (err error) {
Expand Down
24 changes: 21 additions & 3 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,27 @@ func ExampleDelete() {
_ = Set("example-destroy-cache", "my-value", "another-key")

// Delete keys
_, _ = Delete("another-key", "another-key-2")
fmt.Print("deleted keys")
//Output: deleted keys
total, _ := Delete("another-key", "another-key-2")
fmt.Print(total, " deleted keys")
//Output: 2 deleted keys
}

// ExampleDeleteWithoutDependency is an example of DeleteWithoutDependency() method
func ExampleDeleteWithoutDependency() {
// Create a local connection
_ = Connect(connectionURL, maxActiveConnections, maxIdleConnections, maxConnLifetime, idleTimeout, true)

// Disconnect at end
defer Disconnect()

// Set the key/value
_ = Set("example-destroy-cache-1", "my-value")
_ = Set("example-destroy-cache-2", "my-value")

// Delete keys
total, _ := DeleteWithoutDependency("example-destroy-cache-1", "example-destroy-cache-2")
fmt.Print(total, " deleted keys")
//Output: 2 deleted keys
}

// ExampleKillByDependency is an example of KillByDependency() method
Expand Down

0 comments on commit 93ca890

Please sign in to comment.