Commit a4c7654
committed
fix: optimize existsById() to use Redis EXISTS command instead of data retrieval (#657)
The existsById() method in both RedisDocumentRepository and RedisEnhancedRepository
was inefficiently retrieving entire data structures when only existence validation
was needed. This optimization replaces the inherited Spring Data behavior with
direct Redis EXISTS commands, providing significant performance gains.
Changes:
- Override existsById() in SimpleRedisDocumentRepository and SimpleRedisEnhancedRepository
- Use connection.keyCommands().exists() for O(1) existence checks
- Properly handle composite IDs by implementing getKeyForId() helper methods
- Apply ID filters and maintain full backward compatibility
Performance impact:
- Documents: existsById() → findById() → JSON.GET → full document deserialization
- Hashes: existsById() → findById() → HGETALL → full hash retrieval
- After: existsById() → EXISTS → boolean result only
- Eliminates unnecessary network transfer and CPU overhead for existence checks1 parent 3a1a44e commit a4c7654
File tree
2 files changed
+90
-0
lines changed- redis-om-spring/src/main/java/com/redis/om/spring/repository/support
2 files changed
+90
-0
lines changedLines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
722 | 723 | | |
723 | 724 | | |
724 | 725 | | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
725 | 770 | | |
726 | 771 | | |
727 | 772 | | |
| |||
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
596 | 597 | | |
597 | 598 | | |
598 | 599 | | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
599 | 644 | | |
600 | 645 | | |
601 | 646 | | |
| |||
0 commit comments