Git Product home page Git Product logo

Comments (11)

xetorthio avatar xetorthio commented on August 27, 2024

I don't how we can make this possible. If we hide connection pooling within Jedis class, we will have to borrow a connection on every executed command and maybe even deal with synchronization issues. It will make things even harder.
Now we moved to the Apache Pool, take a look into the docs since it has bunch of features that might help you to deal with keeping track of connection usage.
http://commons.apache.org/pool/apidocs/org/apache/commons/pool/impl/GenericObjectPool.html

from jedis.

xetorthio avatar xetorthio commented on August 27, 2024

Or you could use an approach like this:
https://github.com/xetorthio/jedis/issues#issue/36

from jedis.

hamsterready avatar hamsterready commented on August 27, 2024

Or use simple template/callback "pattern" just like jdbctemplate or hibernatetemplate.

Something like:
public class JedisTemplate implements InitializingBean {
private JedisPool jedisPool = null;

  @Override
  public void afterPropertiesSet() throws Exception {
    Assert.notNull(jedisPool, "Property jedisPool is required");
  }

  public <T> T doInJedis(JedisCallback<T> jedisCallback) {
    boolean broken = false;
    Jedis j = null;
    try {
      j = jedisPool.getResource();
      onDoInJedis(j, jedisCallback);
      return jedisCallback.run(j);
    } catch (Exception e) {
      broken = true;
      throw new RuntimeException(e.getMessage(), e);
    } finally {
      if (j != null) {
        if (broken) {
          jedisPool.returnBrokenResource(j);
        } else {
          jedisPool.returnResource(j);
        }
      }
    }

  }

  /**
   * Extension point.
   * 
   * @param j
   * @param jedisCallback
   */
  protected void onDoInJedis(Jedis j, JedisCallback<?> jedisCallback) {
    // do nothing
  }

  public void setJedisPool(JedisPool jedisPool) {
    this.jedisPool = jedisPool;
  }
}

And here is simple callback:

public interface JedisCallback<T> {

  T run(Jedis j);
}

There is spring-data project which has redis support. I was going to use it and have played for a while but had to fall back to above solution as it is much simpler and spring-data is not mature at the moment.

from jedis.

xetorthio avatar xetorthio commented on August 27, 2024

Maybe it make sense to put this in the Wiki or even in a Gist. If lots of people use we could add this to a contrib project, I don't know if I would add this to the core.

from jedis.

hamsterready avatar hamsterready commented on August 27, 2024

Yeah, this is not for core. Just wrapper - and spring data sounds like ultimate solution.

Maybe I will extract my wrapper classes and create spring-jedis or jedis-spring project someday :)

from jedis.

Iker-Jimenez avatar Iker-Jimenez commented on August 27, 2024

I would love to see that. I'm using Spring and Jedis too.
Any clever way of configuring the apache pool from Spring xml?

from jedis.

hamsterready avatar hamsterready commented on August 27, 2024

Oh, I see I need to drop it somewhere:

public class JedisPoolFactoryBean implements FactoryBean<JedisPool>, DisposableBean {

  private String host;
  private int port = Protocol.DEFAULT_PORT;
  private JedisPool pool;

  @Override
  public void destroy() throws Exception {
    pool.destroy();
  }

  @Override
  public JedisPool getObject() throws Exception {
    pool = new JedisPool(host, port);
    pool.init();
    return pool;
  }

  @Override
  public Class<?> getObjectType() {
    return JedisPool.class;
  }

  @Override
  public boolean isSingleton() {
    return true;
  }

  public void setHost(String host) {
    this.host = host;
  }

  public void setPort(int port) {
    this.port = port;
  }

}

and here is xml:

<bean id="jedisPool" class="com.sentaca.support.redis.JedisPoolFactoryBean">
  <property name="host" value="localhost"/>
</bean>

and that's almost 90% in terms of LOC of whole "project".

from jedis.

Iker-Jimenez avatar Iker-Jimenez commented on August 27, 2024

Well, In my case I'm using a ShardedJedisPool, this is with the apache pool, not the old version. I need to set up things like testWhileIdle or maxActive and it looks like I need to use an instance of org.apache.commons.pool.impl.GenericObjectPool.Config for that.

This class doesn't have setters or getters so I had to extend it to provide setters so I could configure an instance and pass as a constructor-arg to my redis.clients.jedis.ShardedJedisPool. I was wondering if there was another way of doing it without extending the Config class.

from jedis.

hamsterready avatar hamsterready commented on August 27, 2024

I would just pull code, fix it and kindly ask mr xetorthio to merge :)

from jedis.

costin avatar costin commented on August 27, 2024

Even better it would be to simply depend only on the ObjectPool interface since one might want to use her own implementation or use the stack-based implementation.
As for Spring configurations, take a look at the Jedis config classes in Spring Data (along with the RedisTemplate & co).

from jedis.

xetorthio avatar xetorthio commented on August 27, 2024

I am closing this issue since it seems we are talking about addind getters and setters to Config object instead of adding a Proxy class.
And I'm still waiting for the pull request! :)

from jedis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.