I want to improve the performance of my Redis read/writes, by reusing the connection object. So, I am making it static in the class, and my get() and set() functions are using this static connection.
In a multithreaded environment, can this cause data corruption? As in, since the connection is static, can 2 threads access it at the same time and data meant for one, be sent to the other?
I am using Lettuce lib in Java.
class RedisServiceLocator { static StatefulRedisConnection<String, String> redisConn = null; // constructor public RedisServiceLocator() { // create client and get connection if null if(RedisServiceLocator.redisConn == null) { RedisClient redisClient = RedisClient.create(redisURL); RedisServiceLocator.redisConn = redisClient.connect() } } // set public void set(String key, String val){ RedisServiceLocator.redisConn.sync().set(key, val); } // get public String get(String key){ redisConn.sync().get(key); }}