@Test public void testRedisMap() { Object object = redisTemplate.execute(new SessionCallback() { @Override public Object execute(RedisOperations operations) throws DataAccessException { operations.multi(); operations.opsForValue().get("test"); operations.delete("test"); operations.opsForValue().set("test", "6"); List<String> rs = operations.exec(); System.out.println(" rs:" + rs.toString()); return rs; } }); List<Object> strings = (List<Object>) object; for (Object str : strings) { System.err.println(str.toString()); } }
public Object put(final String key, final Object value) throws CacheException { final String realKey = key(key); return redisTemplate.execute(new SessionCallback<Object>(){ public Object execute(RedisOperations operations) throws DataAccessException { operations.multi();//事务开启 operations.opsForValue().get(realKey); if(cacheExpire){ operations.opsForValue().set(realKey, value, cacheExpireSeconds, TimeUnit.SECONDS); }else{ operations.opsForValue().set(realKey, value); } List<Object> results = operations.exec(); //结束事务 return results.get(0); } }); }
@Test public void testUseTransactionSimple() { redisTemplate.execute(new SessionCallback<List<Object>>() { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<Object> execute(RedisOperations operations) throws DataAccessException { operations.multi(); operations.opsForValue().set("test1", "value1"); operations.opsForSet().add("testSet1", "value1", "value2"); // 这里获取 test1 为 null,因为事务还没有提交 assertThat(operations.opsForValue().get("test1")).isNull(); return operations.exec(); } }); assertThat(redisTemplate.opsForValue().get("test1")).isEqualTo("value1"); MatcherAssert.assertThat(redisTemplate.opsForSet().members("testSet1"), Matchers.containsInAnyOrder("value1", "value2")); }
@Bean public RedisOperations<String, Request> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Request> template = new RedisTemplate<String, Request>(); template.setConnectionFactory(redisConnectionFactory); RedisSerializer<String> keySerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer<Request> requestSerializer = new Jackson2JsonRedisSerializer<Request>(Request.class); template.setKeySerializer(keySerializer); template.setValueSerializer(requestSerializer); template.setHashKeySerializer(keySerializer); template.setHashValueSerializer(requestSerializer); return template; }
@Test public void qualifiedConnectionFactoryRedisConfig() { registerAndRefresh(RedisConfig.class, QualifiedConnectionFactoryRedisConfig.class); RedisOperationsSessionRepository repository = this.context .getBean(RedisOperationsSessionRepository.class); RedisConnectionFactory redisConnectionFactory = this.context .getBean("qualifiedRedisConnectionFactory", RedisConnectionFactory.class); assertThat(repository).isNotNull(); assertThat(redisConnectionFactory).isNotNull(); RedisOperations redisOperations = (RedisOperations) ReflectionTestUtils .getField(repository, "sessionRedisOperations"); assertThat(redisOperations).isNotNull(); assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory")) .isEqualTo(redisConnectionFactory); }
@Test public void primaryConnectionFactoryRedisConfig() { registerAndRefresh(RedisConfig.class, PrimaryConnectionFactoryRedisConfig.class); RedisOperationsSessionRepository repository = this.context .getBean(RedisOperationsSessionRepository.class); RedisConnectionFactory redisConnectionFactory = this.context .getBean("primaryRedisConnectionFactory", RedisConnectionFactory.class); assertThat(repository).isNotNull(); assertThat(redisConnectionFactory).isNotNull(); RedisOperations redisOperations = (RedisOperations) ReflectionTestUtils .getField(repository, "sessionRedisOperations"); assertThat(redisOperations).isNotNull(); assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory")) .isEqualTo(redisConnectionFactory); }
@Test public void qualifiedAndPrimaryConnectionFactoryRedisConfig() { registerAndRefresh(RedisConfig.class, QualifiedAndPrimaryConnectionFactoryRedisConfig.class); RedisOperationsSessionRepository repository = this.context .getBean(RedisOperationsSessionRepository.class); RedisConnectionFactory redisConnectionFactory = this.context .getBean("qualifiedRedisConnectionFactory", RedisConnectionFactory.class); assertThat(repository).isNotNull(); assertThat(redisConnectionFactory).isNotNull(); RedisOperations redisOperations = (RedisOperations) ReflectionTestUtils .getField(repository, "sessionRedisOperations"); assertThat(redisOperations).isNotNull(); assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory")) .isEqualTo(redisConnectionFactory); }
@Test public void namedConnectionFactoryRedisConfig() { registerAndRefresh(RedisConfig.class, NamedConnectionFactoryRedisConfig.class); RedisOperationsSessionRepository repository = this.context .getBean(RedisOperationsSessionRepository.class); RedisConnectionFactory redisConnectionFactory = this.context .getBean("redisConnectionFactory", RedisConnectionFactory.class); assertThat(repository).isNotNull(); assertThat(redisConnectionFactory).isNotNull(); RedisOperations redisOperations = (RedisOperations) ReflectionTestUtils .getField(repository, "sessionRedisOperations"); assertThat(redisOperations).isNotNull(); assertThat(ReflectionTestUtils.getField(redisOperations, "connectionFactory")) .isEqualTo(redisConnectionFactory); }
public Object remove(final String key) throws CacheException { final String realKey = key(key); return redisTemplate.execute(new SessionCallback<Object>(){ public Object execute(RedisOperations operations) throws DataAccessException { operations.multi();//事务开启 operations.opsForValue().get(realKey); operations.delete(realKey); List<Object> results = operations.exec(); //结束事务 return results.get(0); } }); }
public CustomizedRedisCache(String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration,long preloadSecondTime) { super(name, prefix, redisOperations, expiration); this.redisOperations = redisOperations; this.preloadSecondTime = preloadSecondTime; }
public CustomizedRedisCache(String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration,long preloadSecondTime, boolean allowNullValues) { super(name, prefix, redisOperations, expiration, allowNullValues); this.redisOperations = redisOperations; this.preloadSecondTime = preloadSecondTime; }
public ExpirableRedisCache(String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration) { super(name, prefix, redisOperations, expiration); this.cacheMetadata = new ExpirableRedisCacheMetadata(name, prefix); this.cacheMetadata.setDefaultExpiration(expiration); this.redisOperations = redisOperations; }
@Test public void testDefaultRedisConfiguration() throws Exception { load(); assertThat(this.context.getBean("redisTemplate", RedisOperations.class)) .isNotNull(); assertThat(this.context.getBean(StringRedisTemplate.class)).isNotNull(); }
@Before public void setUp() throws Exception { redisTemplate = mock(RedisTemplate.class); valueOperations = mock(ValueOperations.class); redisConnection = mock(RedisConnection.class); redisOperations = mock(RedisOperations.class); redisConnectionFactory = mock(RedisConnectionFactory.class); when(redisTemplate.opsForValue()).thenReturn(valueOperations); when(redisTemplate.getConnectionFactory()).thenReturn(redisConnectionFactory); when(valueOperations.getOperations()).thenReturn(redisOperations); when(redisTemplate.getConnectionFactory().getConnection()).thenReturn(redisConnection); idempotentRepository = new RedisStringIdempotentRepository(redisTemplate, REPOSITORY); idempotentRepository.setExpiry(1000L); }
@Bean public RedisOperations<String, SecurityCode> redisTemplate(RedisConnectionFactory redisConnectionFactory, HashedStringRedisSerializer hashedStringRedisSerializer, SecureJacksonJsonRedisSerializer<SecurityCode> secureJacksonJsonRedisSerializer) { return CommonConfiguration.redisTemplate(redisConnectionFactory, hashedStringRedisSerializer, secureJacksonJsonRedisSerializer); }
@Bean public RedisOperations<String, AccessInvitations> redisAccessInvitationsTemplate(RedisConnectionFactory redisConnectionFactory, HashedStringRedisSerializer hashedStringRedisSerializer) { return CommonConfiguration.redisTemplate(redisConnectionFactory, hashedStringRedisSerializer, new JacksonJsonRedisSerializer<AccessInvitations>(AccessInvitations.class)); }
private static <T> RedisOperations<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer<String> keySerializer, RedisSerializer<T> valueSerializer) { RedisTemplate<String, T> template = new RedisTemplate<String, T>(); template.setConnectionFactory(redisConnectionFactory); RedisSerializer<String> stringSerializer = new StringRedisSerializer(); template.setKeySerializer(stringSerializer); template.setValueSerializer(valueSerializer); template.setHashKeySerializer(keySerializer); template.setHashValueSerializer(valueSerializer); return template; }
public UpdatableRedisCache( String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration) { super(name, prefix, redisOperations, expiration); this.redisOperations = redisOperations; // self = PropertyAccessorFactory.forDirectFieldAccess(this); this.expirationInSeconds = expiration; }
/** * Creates a new instance. For an example, refer to the class level javadoc. * * @param sessionRedisOperations The {@link RedisOperations} to use for managing the * sessions. Cannot be null. */ public RedisOperationsSessionRepository( RedisOperations<Object, Object> sessionRedisOperations) { Assert.notNull(sessionRedisOperations, "sessionRedisOperations cannot be null"); this.sessionRedisOperations = sessionRedisOperations; this.expirationPolicy = new RedisSessionExpirationPolicy(sessionRedisOperations, this::getExpirationsKey, this::getSessionKey); }
RedisSessionExpirationPolicy(RedisOperations<Object, Object> sessionRedisOperations, Function<Long, String> lookupExpirationKey, Function<String, String> lookupSessionKey) { super(); this.redis = sessionRedisOperations; this.lookupExpirationKey = lookupExpirationKey; this.lookupSessionKey = lookupSessionKey; }
@Override public RedisOperations<String, ?> getOperations() { throw new UnsupportedOperationException("Unsupported method"); }
public CustomerRedisCacheManager(RedisOperations redisOperations) { super(redisOperations); }
public CustomizedRedisCacheManager(RedisOperations redisOperations) { super(redisOperations); }
public CustomizedRedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames) { super(redisOperations, cacheNames); }
public ExpirableRedisCacheManager(RedisOperations redisOperations) { super(redisOperations); }
@SuppressWarnings("rawtypes") public ExpirableRedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames) { super(redisOperations, cacheNames); }
@Test public void testWatch() throws InterruptedException { final String watchKey = "watchKey"; redisTemplate.opsForValue().set(watchKey, "value0"); Thread t1 = new Thread(new Runnable() { @Override public void run() { List<Object> result = redisTemplate.execute(new SessionCallback<List<Object>>() { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public List<Object> execute(RedisOperations operations) throws DataAccessException { operations.watch(watchKey); // watch String oldValue = (String) operations.opsForValue().get(watchKey); String newValue = oldValue + "-11"; operations.multi(); // 开启事物 operations.opsForValue().set(watchKey, newValue); // 修改值 try { Thread.sleep(1000); // 让子线程暂停下,以便其他线程修改 watchKey 的值 } catch (InterruptedException e) { e.printStackTrace(); } return operations.exec(); // 提交事物 } }); assertThat(result).isNull(); // 事物提交失败,返回null,否则会返回一个空 List } }); t1.start(); Thread.sleep(100); redisTemplate.opsForValue().set(watchKey, "value2"); // 主线程先修改 watchKey 的值,子线程提交时会观察到 watchKey 的值已经改变,所以子线程事物提交会失败 t1.join(); assertThat(redisTemplate.opsForValue().get(watchKey)).isEqualTo("value2"); }
public AppCacheManager(RedisOperations redisOperations) { super(redisOperations); this.ops = redisOperations; this.setUsePrefix(true); }
public AppCacheManager(RedisOperations redisOperations, Collection<String> cacheNames) { super(redisOperations, cacheNames); this.ops = redisOperations; this.setUsePrefix(true); }
public RedisOperations<K, V> getOperations() { return ops.getOperations(); }
public DefaultBoundKeyOperations(K key, RedisOperations<K, ?> operations) { setKey(key); this.ops = operations; }
public RedisOperations<K, V> getOperations() { return template; }
public RedisOperations<H, ?> getOperations() { return ops.getOperations(); }
static RedisOperations<String, String> stringTemplate( RedisConnectionFactory redisConnectionFactory) { return new StringRedisTemplate(redisConnectionFactory); }
public CustomRedisCacheManager(RedisOperations redisOperations) { super(redisOperations); }