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

I can't use Second Level Cache with ABP #6672

Open
mardemza opened this issue Mar 8, 2023 · 22 comments
Open

I can't use Second Level Cache with ABP #6672

mardemza opened this issue Mar 8, 2023 · 22 comments

Comments

@mardemza
Copy link

mardemza commented Mar 8, 2023

I cannot implement the Second Level Cache library because it needs to use the service provider and it is not available in the method that configures the
DbContext Configuration.Modules.AbpEfCore().AddDBContext<MyDbContext>(options => ....... ..

And something like this is needed
Configuration.Modules.AbpEfCore().AddDBContext<MyDbContext>((serviceProvider, options) => .......

Can you please help me to find a solution so inside the configurer I can implement this
builder.UseSqlServer(connectionString).AddInterceptors(serviceProvider.GetRequiredService<SecondLevelCacheInterceptor>());

@ismcagdas
Copy link
Member

Maybe you can access serviceProvider in your interceptor by following an approach similar to https://stackoverflow.com/a/67003567

@mardemza
Copy link
Author

My friend, it didn't work :(

My code
image

The service provider is needed here
image

This is documentacion of Second Level Cache Interceptor
image

@mef760
Copy link

mef760 commented Mar 13, 2023

I have the same problem. any suggestion?

@ismcagdas
Copy link
Member

Is it possible to call the code below in your mdoule's PostInitialize method ?

In that case, you can get ServiceCollectionProvider like this;

var serviceCollectionProvider = IocManager.IocContainer.Resolve<IServiceCollectionProvider>();

image

@mardemza
Copy link
Author

@ismcagdas I still can't configure the use of Second Level Cache Interceptor,

Cannot find the IServiceCollectionProvider
image

Do you have any suggestions?

@ismcagdas
Copy link
Member

I think you can register IServiceCollectionProvider as explained in the blog post below and then use it in EF core project;

https://aspnetzero.com/blog/persist-data-protection-keys-to-database

@mardemza
Copy link
Author

I don't work because I need the IServiceProvider and not the IServiceCollectionProvider

@ismcagdas ismcagdas added this to the v8.3 milestone Jun 1, 2023
@ismcagdas
Copy link
Member

I will try to prepare a sample but it might take some time.

@mardemza mardemza changed the title I can't use Second Level Cache I can't use Second Level Cache with ABP Jun 5, 2023
@mardemza
Copy link
Author

mardemza commented Jun 22, 2023

I've tried this, but it doesn't work.

https://support.abp.io/QA/Questions/1430/How-to-add-interceptor-to-EF-Core-db-context#answer-b81252e7-307b-d8af-bba7-39fce208e8a4

Apparently it is not the same Framework so it does not find the AbpDbContextOptions
image

@ismcagdas
Copy link
Member

Yes, it is not same.

@ismcagdas ismcagdas modified the milestones: v8.3, v8.4 Jul 27, 2023
@ismcagdas ismcagdas modified the milestones: v8.4, v9.0 Oct 4, 2023
@ismcagdas ismcagdas modified the milestones: v9.0, v9.1 Nov 15, 2023
@behiunforgiven
Copy link

@mardemza
Did you have any progress on this issue?

@mardemza
Copy link
Author

@behiunforgiven
No, it only works in abp.io project, but in aspnetboilerplate project it still doesn't work

@ismcagdas ismcagdas modified the milestones: v9.1, v9.2 Feb 1, 2024
@ismcagdas ismcagdas self-assigned this Feb 7, 2024
@ismcagdas
Copy link
Member

Have you tried this appraoch ?

public static class Issue6672DbContextConfigurer
{
	public static void Configure(DbContextOptionsBuilder<Issue6672DbContext> builder, string connectionString)
	{
		var interceptor = IocManager.Instance.Resolve<SecondLevelCacheInterceptor>();
		builder.UseSqlServer(connectionString)
			.AddInterceptors(interceptor);
	}

	public static void Configure(DbContextOptionsBuilder<Issue6672DbContext> builder, DbConnection connection)
	{
		var interceptor = IocManager.Instance.Resolve<SecondLevelCacheInterceptor>();
		builder.UseSqlServer(connection)
			.AddInterceptors(interceptor);
	}
}

@behiunforgiven
Copy link

It runs without problem, but caching does not work.

@ismcagdas
Copy link
Member

In my test, I can see that SecondLevelCacheInterceptor is executed. But, I'm not sure why cache doesn't work.

@mardemza
Copy link
Author

@ismcagdas - @behiunforgiven I tried but I didn't have success, it resolves SecondLevelCacheInterceptor well, but then it doesn't cache.

@ismcagdas
Copy link
Member

I will try to check it again.

@ismcagdas ismcagdas modified the milestones: v9.2, v9.3 Apr 24, 2024
@cyrusdowney
Copy link

So this may not be a SecondLevelCache issue. I created an issue with interceptors not firing

#6948

Don't know if it is related or not, but thought I would chime in.

@ismcagdas
Copy link
Member

@mardemza I think I figured it out. Since AppService methods and Controller actions are starting a transaction by default (UnitOfWork), you should use AllowCachingWithExplicitTransactions(true) while configuring second level cache. I tried this and there was no query in SQL Server profiler.

services.AddEFSecondLevelCache(options =>
		options.UseMemoryCacheProvider()
			.ConfigureLogging()
			.AllowCachingWithExplicitTransactions(true)
			.CacheAllQueries(CacheExpirationMode.Sliding, TimeSpan.FromMinutes(60)) // Does not take effect
);

@ismcagdas ismcagdas removed this from the v9.3 milestone May 9, 2024
@mardemza
Copy link
Author

mardemza commented May 9, 2024

@ismcagdas I tried what they told me but it didn't work, it doesn't cache

@ismcagdas
Copy link
Member

I have prepared a sample https://github.com/ismcagdas/Issue6672. Run the project and go to Users page and press the search button several times. You will see that only first request will go to database.

@behiunforgiven
Copy link

when i want to update the database I get this error:

Unable to create a 'DbContext' of type ''. The exception 'No component for supporting the service EFCoreSecondLevelCacheInterceptor.SecondLevelCacheInterceptor was found' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants