1

我有一个视图模型,它代表所有可用于搜索的字段。我想添加一些逻辑,能够识别搜索值是否都相同,并确定是否再次点击数据库进行查询。

我想我必须做类似的事情..

  • 用户提交表单后,将表单值保存到某个临时字段。
  • 在第二次提交时将临时值与表单值集合进行比较。
  • 如果值相等,则在视图模型中设置属性 IsSameSearch = true

我也想使用 Post Redirect Get Pattern。因此,我的搜索视图除了将表单值发布到另一个处理和过滤数据的操作之外,什么都不做,然后使用 Ajax “获取”。

SearchViewModel 包含许多搜索参数。这是一个缩略版。

    public bool UseAdvancedSearch { get; set; }
    public bool isSameSearch { get; set; }
    /// <summary>
    /// Gets or sets the page.
    /// </summary>
    [HiddenInput]
    [ScaffoldColumn(false)]
    public int Page { get; set; }

    [HiddenInput]
    [ScaffoldColumn(false)]
    public string SortOption { get; set; }

    /// <summary>
    ///     Gets or sets the address keywords.
    /// </summary>
    [Display(Name="Address")]
    public string AddressKeywords { get; set; }

    /// <summary>
    ///     Gets or sets the census.
    /// </summary>
    public string Census { get; set; }

    /// <summary>
    ///     Gets or sets the lot block sub.
    /// </summary>
    public string LotBlockSub { get; set; }

    /// <summary>
    ///     Gets or sets the owner keywords.
    /// </summary>
    [Display(Name="Owner")]
    public string OwnerKeywords { get; set; }

    /// <summary>
    ///     Gets or sets the section township range.
    /// </summary>
    public string SectionTownshipRange { get; set; }

    /// <summary>
    ///     Gets or sets the strap.
    /// </summary>
    ///
    [Display(Name="Account Number/Parcel ID")]
    public string Strap { get; set; }

    /// <summary>
    ///     Gets or sets the subdivision.
    /// </summary>
    public string Subdivision { get; set; }

    /// <summary>
    /// Gets or sets the use code.
    /// </summary>
    [Display(Name = "Use Code")] 
    public string UseCode { get; set; }

    /// <summary>
    ///     Gets or sets the zip code.
    /// </summary>
    [Display(Name="Zip Code")]
    public string ZipCode { get; set; }
4

2 回答 2

1

如果您从 Entity Framework 获取数据,则可以在 EF 级别缓存数据。查看扩展包实体框架https://github.com/loresoft/EntityFramework.Extended。就像在您用来检索和过滤数据的查询中添加方法 .FromCache () 一样简单,它将缓存查询结果。确保使用包含等加载所需的所有数据。

您不必担心模型中的相同搜索,因为缓存提供程序会查看过滤器设置并确定它不同。或者在过滤之前缓存数据,然后过滤缓存的结果。如果您有大量具有显着差异的过滤器参数,这更合适,因为您只需缓存 1 个大结果而不是数千个较小的结果。

您可以更高级并指定缓存周期,例如缓存 10 分钟

于 2013-09-17T06:10:08.887 回答
0

What you are describing is called caching.

One way to accomplish that in your scenario would be to implement GetHashCode() in a way that it would take into account all your fields/properties to compute a unique value. That way you can use your Hash as the key entry in your cache, and store the results with that key.

For that actual caching you could just use the MemoryCache class provided by the .Net Framework if you are not deploying to a web farm.

Also, if you are familiar with IoC and DI (such as using Unity), things like this can be implemented as an Interceptor, and only requiring you to add an attribute to the method you'd like to cache. That way you implement caching only once as a cross-cutting concern and not fill up your application code with things like this.

于 2013-09-17T04:22:52.523 回答