Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-cache: improve cache config validation #27726

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions generators/spring-boot/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import cleanupTask from './cleanup.js';
import { serverFiles } from './files.js';
import { askForOptionalItems, askForServerSideOpts, askForServerTestOpts } from './prompts.js';

const { CAFFEINE, EHCACHE, HAZELCAST, INFINISPAN, MEMCACHED, REDIS, NO: NO_CACHE } = cacheTypes;
const { CAFFEINE, EHCACHE, HAZELCAST, INFINISPAN, MEMCACHED, REDIS } = cacheTypes;
const { NO: NO_WEBSOCKET, SPRING_WEBSOCKET } = websocketTypes;
const { CASSANDRA, COUCHBASE, MONGODB, NEO4J, SQL } = databaseTypes;
const { MICROSERVICE, GATEWAY } = applicationTypes;
Expand Down Expand Up @@ -104,13 +104,6 @@ export default class SpringBootGenerator extends BaseApplicationGenerator {

get configuring() {
return this.asConfiguringTaskGroup({
checks() {
const config = this.jhipsterConfigWithDefaults;
if (config.enableHibernateCache && [NO_CACHE, MEMCACHED].includes(config.cacheProvider!)) {
this.log.verboseInfo(`Disabling hibernate cache for cache provider ${config.cacheProvider}`);
this.jhipsterConfig.enableHibernateCache = false;
}
},
feignMigration() {
const { reactive, applicationType, feignClient } = this.jhipsterConfigWithDefaults;
if (feignClient) {
Expand Down
6 changes: 5 additions & 1 deletion generators/spring-cache/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ export default class SpringCacheGenerator extends BaseApplicationGenerator {
get configuring() {
return this.asConfiguringTaskGroup({
configure() {
const { databaseType, reactive } = this.jhipsterConfigWithDefaults;
const { databaseType, reactive, cacheProvider } = this.jhipsterConfigWithDefaults;
if (this.jhipsterConfig.enableHibernateCache && (reactive || databaseType !== 'sql')) {
this.log.verboseInfo(`Disabling hibernate cache for ${reactive ? 'reactive application' : 'non-SQL databases'}`);
this.jhipsterConfig.enableHibernateCache = undefined;
}
if (reactive && cacheProvider !== 'no') {
this.log.error(`Cache provider is not supported in reactive application`);
}
},
});
}
Expand Down
18 changes: 18 additions & 0 deletions lib/jhipster/default-application-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,5 +447,23 @@ describe('jdl - DefaultApplicationOptions', () => {
expect(cacheProviderOption).to.equal('no');
});
});
describe('when the cache option is set to ehcache', () => {
it('should set the enableHibernateCache option to true', () => {
expect(
getDefaultConfigForNewApplication({
cacheProvider: 'ehcache',
}).enableHibernateCache,
).to.equal(true);
});
});
describe('when the cache option is set to memcached', () => {
it('should set the enableHibernateCache option to false', () => {
expect(
getDefaultConfigForNewApplication({
cacheProvider: 'memcached',
}).enableHibernateCache,
).to.equal(false);
});
});
});
});
5 changes: 3 additions & 2 deletions lib/jhipster/default-application-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const { JWT, OAUTH2 } = authenticationTypes;
const { ANGULAR, NO: NO_CLIENT_FRAMEWORK } = clientFrameworkTypes;
const { EHCACHE, HAZELCAST } = cacheTypes;

const NO_CACHE_PROVIDER = cacheTypes.NO;
const { NO: NO_CACHE_PROVIDER, MEMCACHED } = cacheTypes;
const NO_SERVICE_DISCOVERY = serviceDiscoveryTypes.NO;

const { MAVEN } = buildToolTypes;
Expand Down Expand Up @@ -156,7 +156,8 @@ export function getConfigForCacheProvider(options: ApplicationDefaults = {}): Ap
if (options[REACTIVE] || options[CACHE_PROVIDER] === undefined) {
options[CACHE_PROVIDER] = NO_CACHE_PROVIDER;
}
options[ENABLE_HIBERNATE_CACHE] ??= options[DATABASE_TYPE] === SQL && !options[REACTIVE] && options[CACHE_PROVIDER] !== NO_CACHE_PROVIDER;
options[ENABLE_HIBERNATE_CACHE] ??=
options[DATABASE_TYPE] === SQL && !options[REACTIVE] && ![NO_CACHE_PROVIDER, MEMCACHED].includes(options[CACHE_PROVIDER]);
return options;
}

Expand Down
Loading