简体   繁体   English

在Java中管理Redis集合的策略

[英]Strategy to manage Redis collections in Java

I have a web application where I use a relational database and Redis for storing my key/value pairs. 我有一个Web应用程序,在其中使用关系数据库和Redis来存储我的键/值对。 I am using Spring Data Redis in my DAO methods to access Redis collections. 我在DAO方法中使用Spring Data Redis访问Redis集合。 The problem is that having many Redis collections in the application makes it really difficult to manage them. 问题在于,应用程序中有很多Redis集合,这使得管理它们真的很困难。 I believe that each collection should be accessed from one single place only ie writes and reads for one collection should be centralized, but that is not the case in my app currently. 我认为每个集合只能从一个地方访问,即,一个集合的读写应该集中,但是我的应用程序当前不是这种情况。 So I would like to come up with a proper and encapsulated access mechanism for all my Redis collections. 因此,我想为我的所有Redis集合提出一个适当的封装访问机制。

How should I structure my app to accomplish this? 我应该如何构造我的应用程序来完成此任务?

Have a look at org.springframework.data.redis.core.RedisTemplate class. 看一下org.springframework.data.redis.core.RedisTemplate类。 a generic template to access all redis provided data structure 访问所有Redis提供的数据结构的通用模板

Create classes which deals with only one entity at a time. 创建一次仅处理一个实体的类。 For example, if you have domain objects Users and Posts (for a Twitter clone example), you can create UserRepository and PostRepository each dealing with their own entities. 例如,如果您具有域对象“用户”和“帖子”(对于Twitter克隆示例),则可以创建UserRepository和PostRepository,它们各自处理自己的实体。
UserRepository deals with adding users, deleting users, updating user details and so on. UserRepository处理添加用户,删除用户,更新用户详细信息等。 And PostRepository does so for posts. 而PostRepository则用于发布。 You can use these repositories in service classes to achieve business functions. 您可以在服务类中使用这些存储库来实现业务功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM