aliostad / CacheCow

An implementation of HTTP Caching in .NET Core and 4.5.2+ for both the client and the server
MIT License
847 stars 172 forks source link

Question: ICacheResource minimal implementation #211

Closed sag3ll0 closed 6 years ago

sag3ll0 commented 6 years ago

Hi, I try implement ICacheResource

public abstract class BaseDto: ICacheResource
    {
        [Key]
        public virtual long? Id { get; set; }

        public virtual string CreatedBy { get; set; }
        public virtual string ModifiedBy { get; set; }
        public virtual DateTime? CreatedDate { get; set; }
        public virtual DateTime? ModifiedDate { get; set; }

        public BaseDto(){}

        public BaseDto(BaseEntity entity)
        {
            Id = entity.Id;
            CreatedBy = entity.CreatedBy;
            ModifiedBy = entity.ModifiedBy;
            CreatedDate = entity.CreatedDate;
            ModifiedDate = entity.ModifiedDate;
        }

        public TimedEntityTagHeaderValue GetTimedETag()
        {
            return new TimedEntityTagHeaderValue(new DateTimeOffset(ModifiedDate.Value));
        }
    }

But i don't understand what need to next. I use Autofac as DI. BaseDto is base class for CarDto and PlaneDto (i don't want write GetTimedETag in each chield class, becouse it will be same).

aliostad commented 6 years ago

Nothing, it will just work. This will prevent the key to be generated by serialisation. However, your backend will still be called and the item need to be read from your data store.

If you want to protect your backend, you need to implemented your own ITimedETagQueryProvider to check with database whether an item has changed or not.

As this chart shows, implementing ICacheResource has some but not all benefits:

diagram

sag3ll0 commented 6 years ago

Thanks for response. I try debug generate etag by implementing ICacheResource, and this not execute method GetTimedETag in baseDto.

aliostad commented 6 years ago

Does not?? If it does not it could be a bug.

If you are using default extractor, this is the line where it extracts the ETag: https://github.com/aliostad/CacheCow/blob/master/src/CacheCow.Server/ETag/DefaultTimedETagExtractor.cs#L25

Please let me know if you are having issue, as I said it could be a bug or misconfiguration.

sag3ll0 commented 6 years ago

Maybe is not bug, maybe i don't add somewhere something, I use webapi 2, with autofac.

sag3ll0 commented 6 years ago

for work GetTimedETag need add ViewModelType in attribute? [HttpCache(DefaultExpirySeconds = 0, ViewModelType = typeof(Car))]

aliostad commented 6 years ago

Not really if you are not providing your own extractor.

aliostad commented 6 years ago

what was the problem??

aliostad commented 6 years ago

@sag3ll0 can you please explain what the problem was? This helps others that might experience your problem. Thanks