Skip to content

Commit

Permalink
122485: proof of concept caching mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Vannerum committed Jan 9, 2025
1 parent d2cf1da commit 4262a17
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,37 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class DSOWithPoliciesDAOImpl extends AbstractHibernateDAO<DsoWithPolicies> implements DSOWithPoliciesDAO {

private final Map<UUID, DsoWithPolicies> policyCache = new ConcurrentHashMap<>();
boolean isCacheInitialized = false;

public void initializeCache(Context context) throws SQLException {
System.out.println("Initializing location cache...");
long startTime = System.currentTimeMillis();

List<DsoWithPolicies> allDsoWithPolicies = findAllDsosWithPolicies(context);
for (DsoWithPolicies dsoWithPolicies : allDsoWithPolicies) {
policyCache.put(dsoWithPolicies.getDsoId(), dsoWithPolicies);
}

System.out.println("Policy cache initialized with " + policyCache.size() + " entries in " +
(System.currentTimeMillis() - startTime) + "ms");
}

@Override
public DsoWithPolicies findDsoWithPoliciesByDsoId(Context context, UUID dsoId) throws SQLException {
String sql = "SELECT Cast(dso_id as varchar), array_to_string(read, ',') as read, " +
"array_to_string(edit, ',') as edit, array_to_string(admin, ',') as admin " +
"FROM dso_policies_including_inherited_and_implied " +
"WHERE Cast(dso_id as varchar) = :dsoId";
Query query = getHibernateSession(context).createNativeQuery(sql);
// Set postgres sql dialect
query.setParameter("dsoId", dsoId.toString());
return new DsoWithPolicies((Object[]) query.getSingleResult());
if (!isCacheInitialized) {
System.out.println("First time access to location cache. Initializing...");
initializeCache(context);
isCacheInitialized = true;
}
DsoWithPolicies dsoWithPolicies = policyCache.get(dsoId);
return dsoWithPolicies;
}

@Override
Expand All @@ -40,4 +57,14 @@ public List<DsoWithPolicies> findAllDsosWithPolicies(Context context) throws SQL
}
return locations;
}

public void clearCache() {
policyCache.clear();
System.out.println("Location cache cleared.");
}

public void reloadCache(Context context) throws SQLException {
clearCache();
initializeCache(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,43 @@
import javax.persistence.Query;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.concurrent.ConcurrentHashMap;

public class LocationDAOImpl extends AbstractHibernateDAO<Location> implements LocationDAO {

// Thread-safe cache for storing dso_id -> Location mappings
private final Map<UUID, Location> locationCache = new ConcurrentHashMap<>();
boolean isCacheInitialized = false;

/**
* Initializes the cache on startup by loading all locations into the map.
*/
public void initializeCache(Context context) throws SQLException {
System.out.println("Initializing location cache...");
long startTime = System.currentTimeMillis();

List<Location> allLocations = findAllLocations(context);
for (Location location : allLocations) {
locationCache.put(location.getDsoId(), location);
}

System.out.println("Location cache initialized with " + locationCache.size() + " entries in " +
(System.currentTimeMillis() - startTime) + "ms");
}

@Override
public Location findLocationByDsoId(Context context, UUID dsoId) throws SQLException {
String sql = "SELECT Cast(dso_id as varchar), array_to_string(location_comm, ',') as location_comm, " +
"array_to_string(location_coll, ',') FROM locations WHERE Cast(dso_id as varchar) = :dsoId";
Query query = getHibernateSession(context).createNativeQuery(sql);
query.setParameter("dsoId", dsoId.toString());
return new Location((Object[]) query.getSingleResult());
if (!isCacheInitialized) {
System.out.println("First time access to location cache. Initializing...");
initializeCache(context);
isCacheInitialized = true;
}
// Check if the location is already cached
Location location = locationCache.get(dsoId);
return location;
}

@Override
Expand All @@ -37,4 +60,14 @@ public List<Location> findAllLocations(Context context) throws SQLException {
}
return locations;
}
}

public void clearCache() {
locationCache.clear();
System.out.println("Location cache cleared.");
}

public void reloadCache(Context context) throws SQLException {
clearCache();
initializeCache(context);
}
}

0 comments on commit 4262a17

Please sign in to comment.