Last active
December 18, 2015 12:10
-
-
Save jorgeuriarte/5781045 to your computer and use it in GitHub Desktop.
Redis zdiffstore groovy example...
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
@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