component output = false hint = "I provide a super simple wrapper around getting and returning Redis resources." { /** * I initialize the Redis resource wrapper using Jedis 3.3.0. */ public void function init() { variables.jarFiles = [ expandPath( "./lib/jedis-3.3.0/commons-pool2-2.6.2.jar" ), expandPath( "./lib/jedis-3.3.0/jedis-3.3.0.jar" ), expandPath( "./lib/jedis-3.3.0/slf4j-api-1.7.30.jar" ) ]; var jedisPoolConfig = loadClass( "redis.clients.jedis.JedisPoolConfig" ) .init() ; variables.redisClient = loadClass( "redis.clients.jedis.JedisPool" ) .init( jedisPoolConfig, "127.0.0.1" ) ; } // --- // PUBLIC METHODS. // --- /** * I obtain a Redis resource from the Jedis pool and pass it to the given callback. * The results of the callback are returned to the calling context. And, the resource * is safely returned to the Jedis connection pool. * * @callback I am the callback that will receive the Redis resource. */ public any function withRedis( required function callback ) { try { var redis = redisClient.getResource(); return( callback( redis ) ); } finally { redis?.close(); } } // --- // PRIVATE METHODS. // --- /** * I load the given class using the Jedis JAR files. * * @className I am the Java class being loaded. */ private any function loadClass( required string className ) { return( createObject( "java", className, jarFiles ) ); } }