Skip to content

Commit 9b091e2

Browse files
Merge pull request #1305 from romitshah02/redis-cache
fix: store collection relation cache in Redis when enabled
2 parents 448714d + 1e523ea commit 9b091e2

5 files changed

Lines changed: 66 additions & 18 deletions

File tree

‎content-api/content-service/conf/application.conf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ redis.host = "localhost"
397397
redis.port = 6379
398398
redis.maxConnections = 128
399399
redis.enable = false
400+
redis.database.hierarchyRelations.id = 10
400401

401402
#Condition to enable publish locally
402403
content.publish_task.enabled=true

‎content-api/hierarchy-manager/src/main/scala/org/sunbird/managers/RelationManager.scala‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.sunbird.common.exception.{ClientException, ErrorCodes, ResponseCode}
88
import org.sunbird.graph.OntologyEngineContext
99
import org.sunbird.graph.external.store.ExternalStoreFactory
1010
import org.sunbird.telemetry.logger.TelemetryManager
11+
import org.sunbird.utils.content.HierarchyRelationCache
1112

1213
import java.util
1314
import scala.concurrent.{ExecutionContext, Future}
@@ -28,6 +29,8 @@ object RelationManager {
2829
private val primaryKey: java.util.List[String] = java.util.Arrays.asList("relationship_key")
2930
private val propsMapping: Map[String, String] = Map("node_ids" -> "")
3031

32+
private val isRedisEnabled: Boolean = Platform.getBoolean("redis.enable", false)
33+
3134

3235
def updateHierarchyRelationships(request: Request)(implicit oec: OntologyEngineContext, ec: ExecutionContext): Future[Response] = {
3336
val rootId = request.getOrDefault("rootId", "").asInstanceOf[String]
@@ -173,26 +176,33 @@ object RelationManager {
173176
if (CollectionUtils.isEmpty(children)) new java.util.ArrayList[util.Map[String, AnyRef]]() else children
174177
}
175178

179+
private def relationshipKey(rootId: String, identifier: String, relationshipType: String): String =
180+
if (StringUtils.isNotBlank(rootId)) s"$rootId:$identifier:$relationshipType"
181+
else s"$identifier:$relationshipType"
182+
176183
private def storeRelationshipData(
177184
rootId : String,
178185
relationshipType : String,
179186
dataMap : Map[String, List[String]]
180187
)(implicit ec: ExecutionContext): Future[List[Response]] = {
181-
val store = ExternalStoreFactory.getExternalStore(
182-
s"$relationCacheKeyspace.$relationCacheTable", primaryKey)
183-
184-
val futures = dataMap.map { case (identifier, nodeIds) =>
185-
val relationshipKey =
186-
if (StringUtils.isNotBlank(rootId)) s"$rootId:$identifier:$relationshipType"
187-
else s"$identifier:$relationshipType"
188-
189-
store.update(
190-
relationshipKey,
191-
List("node_ids"),
192-
List(nodeIds.asJava.asInstanceOf[AnyRef]),
193-
propsMapping
194-
)
188+
if (isRedisEnabled) {
189+
dataMap.foreach { case (identifier, nodeIds) =>
190+
HierarchyRelationCache.replaceSet(relationshipKey(rootId, identifier, relationshipType), nodeIds)
191+
}
192+
Future.successful(List(ResponseHandler.OK))
193+
} else {
194+
val store = ExternalStoreFactory.getExternalStore(
195+
s"$relationCacheKeyspace.$relationCacheTable", primaryKey)
196+
197+
val futures = dataMap.map { case (identifier, nodeIds) =>
198+
store.update(
199+
relationshipKey(rootId, identifier, relationshipType),
200+
List("node_ids"),
201+
List(nodeIds.asJava.asInstanceOf[AnyRef]),
202+
propsMapping
203+
)
204+
}
205+
Future.sequence(futures.toList)
195206
}
196-
Future.sequence(futures.toList)
197207
}
198-
}
208+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.sunbird.utils.content
2+
3+
import org.slf4j.{Logger, LoggerFactory}
4+
import org.sunbird.cache.util.RedisConnector
5+
import org.sunbird.common.Platform
6+
7+
/**
8+
* Redis-backed store for collection hierarchy relationship data (leaf nodes,
9+
* optional nodes, ancestors). Kept on its own db index — separate from the
10+
* general-purpose RedisCache index — so it lines up with the same dedicated
11+
* index used by knowledge-platform-jobs (redis.database.hierarchyRelations.id)
12+
* and lern-service (hierarchy_relations_redis_index).
13+
*/
14+
object HierarchyRelationCache extends RedisConnector {
15+
16+
private val logger: Logger = LoggerFactory.getLogger(HierarchyRelationCache.getClass.getCanonicalName)
17+
override protected val dbIndex: Int = Platform.getInteger("redis.database.hierarchyRelations.id", 10)
18+
19+
def replaceSet(key: String, data: List[String]): Unit = {
20+
if (isEnabled) {
21+
try {
22+
val jedis = getConnection
23+
try {
24+
jedis.del(key)
25+
data.foreach(entry => jedis.sadd(key, entry))
26+
} catch {
27+
case e: Exception =>
28+
logger.error("Exception Occurred While Saving Set Data to HierarchyRelationCache for Key : " + key + "| Exception is:", e)
29+
} finally returnConnection(jedis)
30+
} catch {
31+
case e: Exception =>
32+
logger.error("Redis Connection/Authentication Error for Key : " + key + "| Exception is:", e)
33+
}
34+
}
35+
}
36+
}

‎knowlg-service/conf/application.conf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ redis.host = "localhost"
471471
redis.port = 6379
472472
redis.maxConnections = 128
473473
redis.enable = false
474+
redis.database.hierarchyRelations.id = 10
474475

475476
#Condition to enable publish locally
476477
content.publish_task.enabled=true

‎platform-core/platform-cache/src/main/scala/org/sunbird/cache/util/RedisConnector.scala‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait RedisConnector {
1212
private val HOST = Platform.getString("redis.host", "localhost")
1313
private val PORT = Platform.getInteger("redis.port", 6379)
1414
private val MAX_CONNECTIONS = Platform.getInteger("redis.maxConnections", 128)
15-
private val INDEX = Platform.getInteger("redis.dbIndex", 0)
15+
protected val dbIndex: Int = Platform.getInteger("redis.dbIndex", 0)
1616
private val PASSWORD: String = Platform.getString("redis.password", "")
1717
protected val isEnabled: Boolean = Platform.getBoolean("redis.enable", false)
1818

@@ -42,7 +42,7 @@ trait RedisConnector {
4242
*/
4343
protected def getConnection: Jedis = if (!isEnabled) null else {
4444
val jedis = jedisPool.getResource
45-
if (INDEX > 0) jedis.select(INDEX)
45+
if (dbIndex > 0) jedis.select(dbIndex)
4646
jedis
4747
}
4848

0 commit comments

Comments
 (0)