use of com.google.common.cache.Cache in project fess by codelibs.
the class ViewHelper method getCachedFacetResponse.
public FacetResponse getCachedFacetResponse(final String query) {
final OptionalThing<FessUserBean> userBean = ComponentUtil.getComponent(FessLoginAssist.class).getSavedUserBean();
final String permissionKey = userBean.map(user -> StreamUtil.stream(user.getPermissions()).get(stream -> stream.sorted().distinct().collect(Collectors.joining("\n")))).orElse(StringUtil.EMPTY);
try {
return facetCache.get(query + "\n" + permissionKey, () -> {
final SearchHelper searchHelper = ComponentUtil.getSearchHelper();
final SearchForm params = new SearchForm() {
@Override
public int getPageSize() {
return 0;
}
@Override
public int getStartPosition() {
return 0;
}
};
params.q = query;
final SearchRenderData data = new SearchRenderData();
searchHelper.search(params, data, userBean);
if (logger.isDebugEnabled()) {
logger.debug("loaded facet data: {}", data);
}
return data.getFacetResponse();
});
} catch (final ExecutionException e) {
throw new FessSystemException("Cannot load facet from cache.", e);
}
}
use of com.google.common.cache.Cache in project gerrit by GerritCodeReview.
the class Reindex method printCacheStats.
private void printCacheStats() {
try (Writer sw = new StringWriter()) {
sw.write("Cache Statistics at the end of reindexing\n");
new CacheDisplay(sw, StreamSupport.stream(cacheMap.spliterator(), false).map(e -> new CacheInfo(e.getExportName(), e.get())).collect(Collectors.toList())).displayCaches();
System.out.print(sw.toString());
} catch (Exception e) {
System.out.format("Error displaying the cache statistics\n" + e.getMessage());
}
}
use of com.google.common.cache.Cache in project calcite-avatica by apache.
the class JdbcMetaTest method testPrepareSetsMaxRows.
@Test
public void testPrepareSetsMaxRows() throws Exception {
final String id = UUID.randomUUID().toString();
final String sql = "SELECT * FROM FOO";
final int maxRows = 500;
final ConnectionHandle ch = new ConnectionHandle(id);
final AtomicInteger statementIdGenerator = new AtomicInteger(0);
JdbcMeta meta = Mockito.mock(JdbcMeta.class);
Connection connection = Mockito.mock(Connection.class);
PreparedStatement statement = Mockito.mock(PreparedStatement.class);
ResultSetMetaData resultSetMetaData = Mockito.mock(ResultSetMetaData.class);
ParameterMetaData parameterMetaData = Mockito.mock(ParameterMetaData.class);
@SuppressWarnings("unchecked") Cache<Integer, StatementInfo> statementCache = (Cache<Integer, StatementInfo>) Mockito.mock(Cache.class);
Mockito.when(meta.getStatementIdGenerator()).thenReturn(statementIdGenerator);
Mockito.when(meta.getStatementCache()).thenReturn(statementCache);
Mockito.when(meta.getConnection(id)).thenReturn(connection);
Mockito.when(connection.prepareStatement(sql)).thenReturn(statement);
Mockito.when(statement.isWrapperFor(AvaticaPreparedStatement.class)).thenReturn(false);
Mockito.when(statement.getMetaData()).thenReturn(resultSetMetaData);
Mockito.when(statement.getParameterMetaData()).thenReturn(parameterMetaData);
// Call the real methods
Mockito.doCallRealMethod().when(meta).setMaxRows(statement, maxRows);
Mockito.doCallRealMethod().when(meta).prepare(ch, sql, maxRows);
meta.prepare(ch, sql, maxRows);
Mockito.verify(statement).setMaxRows(maxRows);
}
use of com.google.common.cache.Cache in project meteo by pierre.
the class StreamResource method getSamplesByRoute.
/**
* Get the data points associated with a field in an Esper query.
* For this to work, you need to add ResourceListener as a publisher and configure it in a stream:
* {
* "name": "Jansky",
* "type": "com.ning.metrics.meteo.publishers.ResourceListener",
* "@class": "com.ning.metrics.meteo.publishers.ResourcePublisherConfig"
* }
*
* @param callback Javascript callback
* @param stream Stream name
* @param attribute the SQL alias of an Esper query
* @return jsonp representation of the data points in memory
*/
@GET
@Path("/{stream}/{attribute}")
@Produces(MediaType.APPLICATION_JSON)
public Response getSamplesByRoute(@QueryParam("callback") @DefaultValue("callback") final String callback, @PathParam("stream") final String stream, @PathParam("attribute") final String attribute) {
Cache<Object, Object> samplesCache = null;
final ResourceListener resourceListener = (ResourceListener) compiler.getPublisherInstances().get(stream);
if (resourceListener != null) {
final Map<String, Cache<Object, Object>> samples = resourceListener.getSamplesCache();
samplesCache = samples.get(attribute);
}
return buildJsonpResponse(attribute, samplesCache, callback);
}
use of com.google.common.cache.Cache in project uPortal by Jasig.
the class RDBMUserLayoutStore method getUserProfileByFname.
@Override
public UserProfile getUserProfileByFname(final IPerson person, final String profileFname) {
Tuple<String, String> key = null;
final Cache<Tuple<String, String>, UserProfile> profileCache = getProfileImportExportCache();
if (profileCache != null) {
key = new Tuple<>(person.getUserName(), profileFname);
final UserProfile profile = profileCache.getIfPresent(key);
if (profile != null) {
return profile;
}
}
logger.debug("Getting profile {} for user {}", profileFname, person.getID());
final int userId = person.getID();
final UserProfile userProfile = jdbcOperations.execute((ConnectionCallback<UserProfile>) con -> {
String query = "SELECT USER_ID, PROFILE_ID, PROFILE_NAME, DESCRIPTION, " + "LAYOUT_ID, STRUCTURE_SS_ID, THEME_SS_ID FROM UP_USER_PROFILE WHERE " + "USER_ID=? AND PROFILE_FNAME=?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, userId);
pstmt.setString(2, profileFname);
try {
logger.debug("getUserProfileByFname(): {} userId: {} profileFname: {}", query, userId, profileFname);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
int profileId = rs.getInt(2);
String profileName = rs.getString(3);
String profileDesc = rs.getString(4);
int layoutId = rs.getInt(5);
if (rs.wasNull()) {
layoutId = 0;
}
int structSsId = rs.getInt(6);
if (rs.wasNull()) {
if (!person.equals(getSystemUser())) {
structSsId = getSystemProfileByFname(profileFname).getStructureStylesheetId();
} else {
String msg = "The system user profile has no structure stylesheet Id.";
throw new IllegalStateException(msg);
}
}
int themeSsId = rs.getInt(7);
if (rs.wasNull()) {
if (!person.equals(getSystemUser())) {
themeSsId = getSystemProfileByFname(profileFname).getThemeStylesheetId();
} else {
String msg = "The system user profile has no theme stylesheet Id.";
throw new IllegalStateException(msg);
}
}
UserProfile userProfile1 = new UserProfile(profileId, profileFname, profileName, profileDesc, layoutId, structSsId, themeSsId);
final Locale[] userLocales = localeStore.getUserLocales(person);
final LocaleManager localeManager = localeManagerFactory.createLocaleManager(person, Arrays.asList(userLocales));
userProfile1.setLocaleManager(localeManager);
return userProfile1;
}
logger.debug("Copying template profile {} to user {}", profileFname, person.getID());
rs.close();
pstmt.close();
int defaultProfileUser = getSystemUser().getID();
IPerson defaultProfilePerson = new PersonImpl();
defaultProfilePerson.setID(defaultProfileUser);
if (defaultProfilePerson.getID() != person.getID()) {
UserProfile templateProfile = getUserProfileByFname(defaultProfilePerson, profileFname);
if (templateProfile != null) {
UserProfile newUserProfile = new UserProfile(templateProfile);
final Locale[] userLocales = localeStore.getUserLocales(person);
newUserProfile.setLayoutId(0);
newUserProfile = addUserProfile(person, newUserProfile);
final LocaleManager localeManager = localeManagerFactory.createLocaleManager(person, Arrays.asList(userLocales));
newUserProfile.setLocaleManager(localeManager);
return newUserProfile;
}
}
throw new RuntimeException("Unable to find User Profile for userId " + userId + " and profile " + profileFname);
}
} finally {
pstmt.close();
}
});
if (profileCache != null) {
profileCache.put(key, userProfile);
}
return userProfile;
}
Aggregations