Last active
December 7, 2016 08:58
-
-
Save andboson/37726aecbfcee90e370ca4085c6bec5e to your computer and use it in GitHub Desktop.
go redis expired watch
This file contains hidden or 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
const REFRESH_EXPIRED_MINUTES = 1 | |
// somewhere (maybe in main.go) run: go WatchExpired(); go RefreshExpired(); | |
func WatchExpired() { | |
services.RClient.ConfigSet("notify-keyspace-events", "Ex") | |
pub := services.RClient.PubSub() | |
pub.PSubscribe("__key*__:*") | |
debug, _ := conf.AppConfig.Bool("debug") | |
for { | |
msg, _ := pub.Receive() | |
log.Printf("[expired] %s", msg ) | |
//.....code...................... | |
} | |
defer pub.Close() | |
} | |
/// refresh expired keys to produce events in time | |
func RefreshExpired() { | |
debug, _ := conf.AppConfig.Bool("debug") | |
refresh_period := conf.AppConfig.UInt("redis.refrech_expired_minutes", REFRESH_EXPIRED_MINUTES) | |
refresh_duration := time.Minute * time.Duration(refresh_period) | |
if debug { | |
Log.Printf("[***] refreshing keys") | |
} | |
services.RClient.Keys(models.WATCHED_PREFIX + ":*") | |
if debug { | |
Log.Printf("[***] refreshing keys end") | |
} | |
go time.AfterFunc(refresh_duration, RefreshExpired) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment