mcarleio    /
                    sciurus-cache-redis
                
                            | 1 | package io.mcarle.sciurus.cache.redis;  | 
            ||
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                                    Code Smell
    
    
    
        introduced 
                            by  
        
   Loading history...
                 | 
                |||
| 2 | |||
| 3 | import io.lettuce.core.RedisClient;  | 
            ||
| 4 | import io.lettuce.core.SetArgs;  | 
            ||
| 5 | import io.lettuce.core.api.StatefulRedisConnection;  | 
            ||
| 6 | import io.lettuce.core.api.sync.RedisCommands;  | 
            ||
| 7 | import io.mcarle.sciurus.ExecutionIdentifier;  | 
            ||
| 8 | import io.mcarle.sciurus.cache.CustomCache;  | 
            ||
| 9 | |||
| 10 | import java.io.Serializable;  | 
            ||
| 11 | import java.time.Duration;  | 
            ||
| 12 | |||
| 13 | public class RedisCache implements CustomCache { | 
            ||
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 14 | |||
| 15 | private RedisClient redisClient;  | 
            ||
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 16 | private StatefulRedisConnection<String, RedisData> connection;  | 
            ||
| 17 | private RedisCommands<String, RedisData> syncCommands;  | 
            ||
| 18 | |||
| 19 | 1 |     public RedisCache(RedisClient redisClient) { | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 20 | 1 | this.redisClient = redisClient;  | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 21 | 1 | this.connection = redisClient.connect(new RedisJsonCodec());  | 
            |
| 22 | 1 | this.syncCommands = connection.sync();  | 
            |
| 23 | 1 | }  | 
            |
| 24 | |||
| 25 | @Override  | 
            ||
| 26 |     public Object get(ExecutionIdentifier executionIdentifier) { | 
            ||
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 27 | 1 | RedisData redisData = syncCommands.get(getKey(executionIdentifier));  | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 28 | 1 |         if (redisData == null) { | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 29 | 1 | return CustomCache.EMPTY;  | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 30 |         } else { | 
            ||
| 
                                                                                                                                                        
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 31 | 1 | return redisData.getCachedValue();  | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 32 | }  | 
            ||
| 33 | }  | 
            ||
| 34 | |||
| 35 | @Override  | 
            ||
| 36 |     public void put(ExecutionIdentifier executionIdentifier, Serializable result, Duration duration) { | 
            ||
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 37 | 1 | syncCommands.set(getKey(executionIdentifier), new RedisData(result), SetArgs.Builder.px(duration.toMillis()));  | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 38 | 1 | }  | 
            |
| 39 | |||
| 40 | @Override  | 
            ||
| 41 |     public void postDeregister() { | 
            ||
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 42 | 1 | connection.close();  | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 43 | 1 | redisClient.shutdown();  | 
            |
| 44 | 1 | }  | 
            |
| 45 | |||
| 46 |     private String getKey(ExecutionIdentifier executionIdentifier) { | 
            ||
| 
                                                                                                                                                        
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 47 | 1 | return executionIdentifier.toString() + executionIdentifier.hashCode();  | 
            |
| 
                                                                                                    
                         0 ignored issues 
                            –
                            show
                         | 
                |||
| 48 | }  | 
            ||
| 49 | }  | 
            ||
| 50 |