|
22 | 22 | import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
|
23 | 23 | import org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository;
|
24 | 24 | import org.springframework.data.redis.core.PartialUpdate;
|
| 25 | +import org.springframework.data.redis.core.RedisCallback; |
25 | 26 | import org.springframework.data.redis.core.RedisTemplate;
|
26 | 27 | import org.springframework.data.redis.core.convert.RedisData;
|
27 | 28 | import org.springframework.data.redis.core.convert.ReferenceResolverImpl;
|
@@ -596,6 +597,50 @@ public <S extends T> boolean exists(Example<S> example) {
|
596 | 597 | return count(example) > 0;
|
597 | 598 | }
|
598 | 599 |
|
| 600 | + @Override |
| 601 | + public boolean existsById(ID id) { |
| 602 | + Assert.notNull(id, "The given id must not be null"); |
| 603 | + |
| 604 | + // Use direct Jedis EXISTS command for optimal performance |
| 605 | + // Construct key properly for composite IDs |
| 606 | + String fullKey = getKeyForId(id); |
| 607 | + |
| 608 | + return Boolean.TRUE.equals(modulesOperations.template().execute((RedisCallback<Boolean>) connection -> connection |
| 609 | + .keyCommands().exists(fullKey.getBytes()))); |
| 610 | + } |
| 611 | + |
| 612 | + private String getKeyForId(Object id) { |
| 613 | + // Get the mapping context's entity info |
| 614 | + RedisEnhancedPersistentEntity<?> persistentEntity = (RedisEnhancedPersistentEntity<?>) mappingConverter |
| 615 | + .getMappingContext().getRequiredPersistentEntity(metadata.getJavaType()); |
| 616 | + |
| 617 | + String stringId; |
| 618 | + |
| 619 | + // Handle composite IDs |
| 620 | + if (persistentEntity.isIdClassComposite()) { |
| 621 | + BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(id); |
| 622 | + List<String> idParts = new ArrayList<>(); |
| 623 | + for (RedisPersistentProperty idProperty : persistentEntity.getIdProperties()) { |
| 624 | + Object propertyValue = wrapper.getPropertyValue(idProperty.getName()); |
| 625 | + if (propertyValue != null) { |
| 626 | + idParts.add(propertyValue.toString()); |
| 627 | + } |
| 628 | + } |
| 629 | + stringId = String.join(":", idParts); |
| 630 | + } else { |
| 631 | + stringId = mappingConverter.getConversionService().convert(id, String.class); |
| 632 | + } |
| 633 | + |
| 634 | + // Apply ID filters if they exist |
| 635 | + var maybeIdentifierFilter = indexer.getIdentifierFilterFor(metadata.getJavaType()); |
| 636 | + if (maybeIdentifierFilter.isPresent()) { |
| 637 | + IdentifierFilter<String> filter = (IdentifierFilter<String>) maybeIdentifierFilter.get(); |
| 638 | + stringId = filter.filter(stringId); |
| 639 | + } |
| 640 | + |
| 641 | + return getKeyspace() + stringId; |
| 642 | + } |
| 643 | + |
599 | 644 | // -------------------------------------------------------------------------
|
600 | 645 | // Query By Example Fluent API - QueryByExampleExecutor
|
601 | 646 | // -------------------------------------------------------------------------
|
|
0 commit comments