Created
April 27, 2020 23:32
-
-
Save joshdurbin/d2955b27125a74e6dd3a6dde4cc1d201 to your computer and use it in GitHub Desktop.
2020-2-groovy-concurrent-redis-operations-with-jedis
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30') | |
]) | |
import redis.clients.jedis.JedisPool | |
def jedisPool = new JedisPool() |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30'), | |
@Grab(group='org.apache.commons', module='commons-pool2', version='2.8.0') | |
]) | |
import java.util.concurrent.ConcurrentLinkedQueue | |
import java.util.concurrent.atomic.AtomicBoolean | |
import redis.clients.jedis.JedisPool | |
import org.apache.commons.pool2.impl.GenericObjectPoolConfig | |
def numThreads = 10 | |
def pipelineSize = 200 | |
def queue = new ConcurrentLinkedQueue() | |
def finished = new AtomicBoolean(false) | |
def config = new GenericObjectPoolConfig() | |
config.setMaxTotal(numThreads) | |
def jedisPool = new JedisPool(config) | |
numThreads.times { | |
Thread.start { | |
while (true) { | |
def item = queue.poll() | |
if (item) { | |
jedisPool.getResource().withCloseable { jedis -> | |
def pipeline = jedis.pipelined() | |
def pipelineCount = 0 | |
while (item) { | |
pipeline.set("${item}", 'some val') | |
if (++pipelineCount < pipelineSize) { | |
item = queue.poll() | |
} else { | |
item = null | |
} | |
} | |
pipeline.sync() | |
} | |
} else if (finished.get()) { | |
break | |
} | |
} | |
} | |
} | |
100_000.times { number -> | |
queue.offer(number) | |
} | |
finished.set(true) |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30') | |
]) | |
import redis.clients.jedis.JedisPool | |
def jedisPool = new JedisPool() | |
def jedis = jedisPool.getResource() | |
jedis.set('thing', 'value') |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30') | |
]) | |
import redis.clients.jedis.JedisPool | |
def jedisPool = new JedisPool() | |
jedisPool.getResource().withCloseable { jedis -> | |
jedis.set('thing', 'value') | |
} |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30') | |
]) | |
import redis.clients.jedis.JedisPool | |
def jedisPool = new JedisPool() | |
jedisPool.getResource().withCloseable { jedis -> | |
100_000.times { number -> | |
jedis.set("${number}", 'some val') | |
} | |
} |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30') | |
]) | |
import redis.clients.jedis.JedisPool | |
def jedisPool = new JedisPool() | |
10.times { outerCounter -> | |
Thread.start { | |
jedisPool.getResource().withCloseable { jedis -> | |
10_000.times { innerCounter -> | |
jedis.set("${innerCounter}${outerCounter}", 'some val') | |
} | |
} | |
} | |
} |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30') | |
]) | |
import redis.clients.jedis.JedisPool | |
def jedisPool = new JedisPool() | |
100.times { outerCounter -> | |
Thread.start { | |
jedisPool.getResource().withCloseable { jedis -> | |
1_000.times { innerCounter -> | |
jedis.set("${innerCounter}${outerCounter}", 'some val') | |
} | |
} | |
} | |
} |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30') | |
]) | |
import redis.clients.jedis.JedisPool | |
def jedisPool = new JedisPool() | |
100.times { outerCounter -> | |
Thread.start { | |
1_000.times { innerCounter -> | |
jedisPool.getResource().withCloseable { jedis -> | |
jedis.set("${innerCounter}${outerCounter}", 'some val') | |
} | |
} | |
} | |
} |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30'), | |
@Grab(group='org.apache.commons', module='commons-pool2', version='2.8.0') | |
]) | |
import org.apache.commons.pool2.impl.GenericObjectPoolConfig | |
import redis.clients.jedis.JedisPool | |
def config = new GenericObjectPoolConfig() | |
config.setMaxTotal(100) | |
def jedisPool = new JedisPool(config) | |
100.times { outerCounter -> | |
Thread.start { | |
1_000.times { innerCounter -> | |
jedisPool.getResource().withCloseable { jedis -> | |
jedis.set("${innerCounter}${outerCounter}", 'some val') | |
} | |
} | |
} | |
} |
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
#!/usr/bin/env groovy | |
@Grapes([ | |
@Grab(group='redis.clients', module='jedis', version='3.2.0'), | |
@Grab(group='org.slf4j', module='slf4j-nop', version='1.7.30'), | |
@Grab(group='org.apache.commons', module='commons-pool2', version='2.8.0') | |
]) | |
import redis.clients.jedis.JedisPool | |
import org.apache.commons.pool2.impl.GenericObjectPoolConfig | |
def config = new GenericObjectPoolConfig() | |
config.setMaxTotal(100) | |
def jedisPool = new JedisPool(config) | |
100.times { outerCounter -> | |
Thread.start { | |
10.times { innerCounter -> | |
jedisPool.getResource().withCloseable { jedis -> | |
def pipeline = jedis.pipelined() | |
100.times { pipelinedCounter -> | |
pipeline.set("${innerCounter}${outerCounter}${pipelinedCounter}", 'some val') | |
} | |
pipeline.sync() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment