Skip to content

Instantly share code, notes, and snippets.

@tanopwan
Created March 23, 2021 02:12
Show Gist options
  • Save tanopwan/178291dceec454f44a0d5d5d285f18b1 to your computer and use it in GitHub Desktop.
Save tanopwan/178291dceec454f44a0d5d5d285f18b1 to your computer and use it in GitHub Desktop.
Groovy script to set keys to redis using pipeline in {batchSize} for {batchCount} times
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import groovy.time.TimeCategory
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
Jedis jedis = pool.getResource();
def repeat = vars.get("batchCount").toInteger();
def size = vars.get("batchSize").toInteger();
def r = {
Pipeline pipeline = jedis.pipelined();
def c = {
currentDate = new Date()
println currentDate
use( TimeCategory ) {
before30Mins = currentDate - 30.minutes
}
def key = before30Mins.format('yyMMddhhMMSS')
def payload = groovy.json.JsonOutput.toJson([
data:'prefix' + it
])
pipeline.set(key + it.mod(10000).toString().padLeft(4,'0'), payload);
}
1.upto(size, c)
pipeline.sync();
}
1.upto(repeat, r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment