asp.net - Object references and cache -


i have function getallproducts() fetches products database , stores in cache future requests. works fine, if call function e.g. productsearchresults = getallproducts(), , modify productsearchresults variable, modifies cache, important never happens, cache affects whole website.

i understand because both productsearchresults , cache have same reference, how solve problem? there can put in getallproducts() ensure cache uses own value?

 public shared function getallproducts() productcollection          dim products new productcollection()          if isnothing(system.web.httpcontext.current.cache("productdata"))              '////// database code products goes here //////              system.web.httpcontext.current.cache.insert("productdata", products, nothing, datetime.now.addminutes(5), timespan.zero)         end if         products = system.web.httpcontext.current.cache("productdata")          return products      end function   public shared function searchproducts(byval searchtext string) productcollection          dim productsearchresults productcollection = nothing          if searchtext <> ""              searchtext = searchtext.tolower()              dim keywords new arraylist()             keywords.addrange(searchtext.split(" ".tochararray(), stringsplitoptions.removeemptyentries))              productsearchresults = getallproducts()              integer = 0 keywords.count - 1                  j integer = productsearchresults.count - 1 0 step -1                     if productsearchresults(j).productname.tolower.contains(keywords(i)) = false                         productsearchresults.removeat(j)                     end if                 next              next          end if          return productsearchresults      end function 

this because returning collection of pointers object thats in cache. implement iclonable on object , have function returns new collection cloned objects.

public function getclonedobjects() productcollection  dim mycollection new list(of myobject)  each item product in getproducts()    mycollection.add(item.clone) loop  return mycollection  end function 

or create property hold cloned copy of collection

    private _clonedproducts productcollection = nothing     public readonly property clonedproducts productcollection               if _clonedproducts nothing           _clonedproducts = new productcollection           each item product in getallproducts()             _clonedproducts.add(item.clone())           next         end if         return _clonedproducts       end     end property 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -