Skip to content

Instantly share code, notes, and snippets.

@jorgeuriarte
Last active December 18, 2015 12:10
Show Gist options
  • Save jorgeuriarte/5781045 to your computer and use it in GitHub Desktop.
Save jorgeuriarte/5781045 to your computer and use it in GitHub Desktop.
Redis zdiffstore groovy example...
@Grab(group='redis.clients', module='jedis', version='2.1.0')
import redis.clients.jedis.*
def poolconfig = new JedisPoolConfig()
def conn = (new JedisPool(poolconfig, "localhost", Protocol.DEFAULT_PORT, 0 /* Protocol.DEFAULT_TIMEOUT */, null, 0)).getResource()
def setupRestricciones(connx) {
[31276:1369774800001, /* Scores should be consistant with target zset's internal scores (categoria:78:eventos) */
38000:1412456400000,
37909:1369776600000].each { k, v -> connx.zadd('tmp:restricciones', v, k.toString()) }
connx.rename('tmp:restricciones', 'restricciones')
}
/* Da funktion */
private def zdiffstore(connx, newset, String... tablas) {
def params = new ZParams()
def weights = [0]
tablas.tail().each { it ->
weights << 1
}
params.weights(weights as int[])
connx.zunionstore("tmp:${newset}", params, tablas)
connx.zremrangeByScore("tmp:${newset}", "1", "+inf")
connx.rename("tmp:${newset}", newset)
return connx.zrange(newset, 0, -1)
}
/* Example setup, discard... */
setupRestricciones(conn)
/* Sample usage */
zdiffstore(conn, "resultado", "categoria:78:eventos", "restricciones")
conn.expire("resultado", 1)
conn.zrange("resultado", 0, -1).size()
/* End of sample usage */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment