简体   繁体   English

Redis:我可以将jedis实例初始化为静态的final字段吗?

[英]Redis : Can I init jedis instance as a static final field?

I need to use Redis as data source in Java, so I decide to use the code: 我需要将Redis用作Java中的数据源,因此决定使用以下代码:

public class RedisService {
    private static final Jedis jedis = new Jedis("host",6400);;

    public static Device getDevice(String key) {
        // Do something use redis.
        return null;
    }

}

I thought the server will automatically init Jedis(Redis API for Java), it this a good way to use Jedis ? 我以为服务器会自动初始化Jedis(Java的Redis API),这是使用Jedis的好方法吗?

Have a look at how we are using Jedis: 看看我们如何使用Jedis:

  1. Create a singleton org.springframework.data.redis.connection.jedis.JedisConnectionFactory instance by passing host and port info 通过传递主机和端口信息来创建单例org.springframework.data.redis.connection.jedis.JedisConnectionFactory实例

  2. Create singleton org.springframework.data.redis.core.RedisTemplate instance by passing the connection factory to it 通过将连接工厂传递给它来创建单例org.springframework.data.redis.core.RedisTemplate实例

  3. Use the redisTemplate created above in your service, the benefit of using Redistemplate is that you can use it perform operation across all data structures provided by redis( list, set, hashes ) 使用上面在您的服务中创建的redisTemplate,使用Redistemplate的好处是您可以使用它对redis提供的所有数据结构( list,set,hash )执行操作。

Just for your reference, here's the spring code that does the same, you can use if your are using spring else you can create the same using java code 仅供参考,以下是执行相同操作的spring代码,如果您使用的是spring,可以使用其他代码,也可以使用java代码创建相同的代码。

<!-- Create Factory -->
<bean id="jedisFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
   <property name="hostName" value="localhost" />
   <property name="port" value="6370" />
   <property name="timeout" value="5000" />
</bean> 

<!-- Create Redis Template -->
<bean id="redisRemplate" class="org.springframework.data.redis.core.RedisTemplate" >
    <property name ="connectionFactory" ref="jedisFactory" />
</bean>

<!-- Your Service class -->
<bean id="serviceClass" class="RedisService" >
    <property name ="redisTemplate" ref="redisRemplate" />
</bean>

public class RedisService 
{
    private final RedisTemplate redisTemplate = /* get from  store or  inject using spring */;

    public static Device getDevice(String key) {
        // Do something use Redis.
        return null;
    }
}

As Santosh Joshi tried to explain: it is best to use a JedisFactory. 正如Santosh Joshi试图解释的那样:最好使用JedisFactory。 Your Jedis which is Singleton can "die" due to network, overload etc... and you will have to restart your application to get a new connection to Redis. 由于网络,过载等原因,您的Singleton Jedis可能会“死亡”,并且您将必须重新启动应用程序才能获得与Redis的新连接。

To counter that, you can define a Jedis Pool and, if you don't want to use Spring (on which the solution from Santosh is based on), you can use the JedisPool class which is provided with Jedis. 为了解决这个问题,可以定义一个Jedis Pool,如果不想使用Spring(Santosh的解决方案所基于的Spring),则可以使用Jedis提供的JedisPool类。 Then, you can define it as a singleton (as static final or via Spring for instance) and get Jedis instances from it. 然后,您可以将其定义为单例(例如,通过static final或通过Spring),并从中获取Jedis实例。

As it is a pool you can get more than 1 connection to Redis at a time (you can configure that), and it supports dealing with broken connections: it creates fresh new Jedis when one is dead. 因为它是一个池,所以您一次可以与Redis建立多个连接(可以配置),它支持处理断开的连接:当一个连接死了时,它会创建新的新Jedis。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM