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

Can cached objects be cached per user? #66

Open
chadbengen opened this issue Oct 27, 2016 · 2 comments
Open

Can cached objects be cached per user? #66

chadbengen opened this issue Oct 27, 2016 · 2 comments
Assignees

Comments

@chadbengen
Copy link

I am wondering if the cache feature can be used for disconnected entities in MVC where api caches an entity on load, and then uses that cached entity when the user saves their changes. Thus allowing state to be maintained on the server and to persist only changes.

If this is a valid use, is there a way to cache objects per user? The wiki states that the key is a combination of prefix, tags and expression. So can I cache with the User Id as the tag?

var customer= ctx.Customer.Where(c => c.Id == customerId).FromCache(userId.ToString()));

@zzzprojects zzzprojects self-assigned this Oct 28, 2016
@zzzprojects
Copy link
Collaborator

Hello @chadbengen ,

Disconnected Entities
I'm not sure to understand what you are saying fully. From what I understand, you want to save the cached entities.

The problem with caching in this version is the entity is not the same through all cache. By example:

  • You cache info about the logged customer
  • You cache info about all customers

The logged customer are found in the two cache, but it's not the same object. So modification you will apply on the logged customer will not propagate in the other cache (for the logged customer).

Cache Per User
Yes, you can do it. However, make sure you only cache very few entities if you are going to do it.

Let me know if I answered correctly to your questions

@chadbengen
Copy link
Author

chadbengen commented Oct 28, 2016

Yes, I think you answered my question. To clarify, my thought is to cache single objects so that when the user wants to persist changes, i can call the object from the cache rather than from the db and update the entity that way. With web api, when the user fetches a customer i would call:

Public CustomerDto GetCustomer(int customerId)
{
//clear cache for user id and customer
QueryCacheManager.ExpireTag("customer", userId.ToString());

//generate a guid for the new cache key
var guid = Guid.NewGuid;

//fetch customer and cache it for five minutes
var customer = ctx.Customers.Where(c => c.Id == customerId).FromCache(DateTime.Now.AddMinutes(5),"customer", userId.ToString(), guid.ToString());
...
//map to dto
var customerDto = customerDto.Create(customer)

//set guid on dto
customerDto.cacheKey = guid;

return customerDto;
}

Then when the customer submits changes, the api would call:

Public bool SaveCustomer(CustomerDto customerDto)
{
//fetch the customer from cache, if exists, for the userId and the cacheKey on the dto
var customer = ctx.Customers.Where(c => c.Id == customerDto.Id).FromCache("customer", userId.ToString(), customerDto.cacheKey);

//do i need to attach the entity to the ctx, or does it auto attach to the context when i call it?

//update entity
customer.updateFromDto(customerDto);
...
}

Since i'm pulling the customer from the cache, i save a trip to the db, improving performance for the user when they save changes.

Are there other considerations for this scenario other than memory concerns? There can be a considerable amount of users (30 to 50 users), and they could be working on multiple customers in separate browser tabs.

@zzzprojects zzzprojects removed their assignment Jan 2, 2017
@JonathanMagnan JonathanMagnan self-assigned this Jan 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants