yibu239 / dropthings

Automatically exported from code.google.com/p/dropthings
0 stars 0 forks source link

AspectF problem with cachelist implementation #249

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. search widget by its name
2.
3.

What is the expected output? What do you see instead?
the widgets which are matched with the search name should be searched. But 
every widgets are searched and returned even if some widgets are not matched 
with search name characters.

What version of the product are you using? On what operating system?
version 2.7.6 , windows server 2008 r2

Please provide any additional information below.

I added search function to the dropthings.data class.
It uses AspectF and cachelist function to return data from the database.

It doesn't function at all on searching widgets.

I added this query on the CompiledQueries.cs file.

public static readonly Func<DropthingsDataContext, int, 
Enumerations.WidgetType, string, IQueryable<Widget>> GetSearchWidgetsFast =
                Compile<int, Enumerations.WidgetType, string, Widget>((dc, page, widgetType, search) =>
                    (from widget in dc.Widgets
                     where widget.WidgetType == (int)widgetType 
                        && !((from widgetIN in dc.Widgets
                               where widgetIN.WidgetType == (int)widgetType && widgetIN.Name.StartsWith(search)
                               orderby widgetIN.ID
                               select widgetIN.ID).Take(30 * page)).Contains(widget.ID)
                        && widget.Name.StartsWith(search)
                     orderby widget.ID
                     select widget).Take(30)
                );

and added this to the WidgetRepository.cs

public List<Widget> GetAllWidgetsFast(int page, Enumerations.WidgetType 
widgetType, string searchWidget)
        {
            // 20120612 kblee added it for enhancing the speed of the query.

            if(string.IsNullOrEmpty(searchWidget))
            {                            
                return AspectF.Define
                .CacheList<Widget, List<Widget>>(_cacheResolver, CacheKeys.WidgetKeys.WidgetsByType((int)widgetType),
                w => CacheKeys.WidgetKeys.Widget(w.ID))
                .Return<List<Widget>>(() =>
                     _database.Query(CompiledQueries.WidgetQueries.GetAllWidgetsFast, page, widgetType)
                    .ToList());
            }

            if (GetWidgetsCount(widgetType, searchWidget) <= 0) // if there is no widget to return, then, return default widget
            {
                return new List<Widget>();
            }

            // 20120619 search by widget name is not working because of the cache of AspectF function.
            // It should be resolved later.
            return AspectF.Define
                .CacheList<Widget, List<Widget>>(_cacheResolver, CacheKeys.WidgetKeys.WidgetsByType((int)widgetType),
                w => CacheKeys.WidgetKeys.Widget(w.ID))
                .Return<List<Widget>>(() =>
                    _database.Query(CompiledQueries.WidgetQueries.GetSearchWidgetsFast, page, widgetType, searchWidget)
                    .ToList());

        }

and this one has error.

return AspectF.Define
                .CacheList<Widget, List<Widget>>(_cacheResolver, CacheKeys.WidgetKeys.WidgetsByType((int)widgetType),
                w => CacheKeys.WidgetKeys.Widget(w.ID))
                .Return<List<Widget>>(() =>
                    _database.Query(CompiledQueries.WidgetQueries.GetSearchWidgetsFast, page, widgetType, searchWidget)
                    .ToList());

It returns all widgets when one of the widgets are matched with search name. It 
should return widgets which match with search name only, but it also return 
widgets which don't match with search name.

I think it is related with AspectF and CacheList function in it. I don't know 
what is the problem.

based on the picture i uploaded, when i search the widgets by alphabet 'b', it 
returns blade&soul and gamehigh. but gamehigh does not start with alphabet 'b'. 
so It returns a widget which is not related with search name.

Could you notice it?

Original issue reported on code.google.com by DevFor...@gmail.com on 22 Sep 2012 at 2:20

Attachments:

GoogleCodeExporter commented 8 years ago
It is most likely due to the cache key. The cache key you are using to cache 
the list holds all the widgets of a certain type. You need to remove the cache 
completely because search criteria is dynamic. 

Alternatively you can just read the entire list of widget from cache and then 
run through the widget list, apply the search criteria and return the matched 
set. Hitting database on every search won't be a good choice. Since the widget 
list is small, keep all in memory and match from in-memory list and return the 
matched subset. 

Original comment by omaralzabir@gmail.com on 22 Sep 2012 at 10:14