component
	output = false
	hint = "I define the application settings and event handlers."
	{

	// Configure the application runtime.
	this.name = "RedisListWithJsonExploration";
	this.applicationTimeout = createTimeSpan( 0, 1, 0, 0 );
	this.sessionManagement = false;

	// Setup the mappings for our path evaluation.
	this.webrootDir = getDirectoryFromPath( getCurrentTemplatePath() );
	this.mappings = {
		"/": this.webrootDir,
		"/javaloader": "#this.webrootDir#vendor/JavaLoader/javaloader/",
		"/JavaLoaderFactory": "#this.webrootDir#vendor/JavaLoaderFactory/",
		"/jedis": "#this.webrootDir#vendor/jedis-2.9.3/"
	};

	// ---
	// EVENT METHODS.
	// ---

	/**
	* I get called once when the application is being initialized.
	*/
	public boolean function onApplicationStart() {

		var javaLoaderFactory = new JavaLoaderFactory.JavaLoaderFactory();

		application.javaLoaderForJedis = javaLoaderFactory.getJavaLoader([
			expandPath( "/jedis/commons-pool2-2.4.3.jar" ),
			expandPath( "/jedis/jedis-2.9.3.jar" ),
			expandPath( "/jedis/slf4j-api-1.7.22.jar" )
		]);

		var jedisConfig = application.javaLoaderForJedis
			.create( "redis.clients.jedis.JedisPoolConfig" )
			.init()
		;

		application.jedisPool = application.javaLoaderForJedis
			.create( "redis.clients.jedis.JedisPool" )
			.init( jedisConfig, "127.0.0.1" )
		;

		return( true );

	}


	/**
	* I get called once at the start of each request.
	*/
	public boolean function onRequestStart() {

		request.withRedis = this.withRedis;

		return( true );

	}

	// ---
	// PUBLIC METHODS.
	// ---

	/**
	* I invoke the given Callback with an instance of a Redis connection from the Jedis
	* connection pool. The value returned by the Callback is passed-back up to the
	* calling context. This removes the need to manage the connection in the calling
	* context.
	* 
	* @callback I am a Function that is invoked with an instance of a Redis connection.
	*/
	public any function withRedis( required function callback ) {

		try {

			var redis = application.jedisPool.getResource();

			return( callback( redis ) );

		} finally {

			redis?.close();

		}

	}

}