I just started using Redis and I'm using it for one of my personal projects. The Redis DB contains about 10k objects of
public Class FileList
{
public string FileName { get; set;}
public string FolderName { get; set;}
}
I'm displaying this list in the ListBox ResultsView
initially on load. I've a TextBox
where I can enter characters and on the TextBoxChanged_Event
I'm calling a function which will query the RedisDB for all the FileList
objects containing the character I typed in the TextBox
and store it in a ResultsList<FileList>
which will be looped in a foreach
and then added to the ListBox
.
It takes at least a second to start displaying results and is not very fast.
Now, If I do a same query on a MasterList<FileList>
, then it's a bit faster but still not fast enough.
Yesterday, I tried with RavenDb which was painstakingly taking long times to do the same tasks.
Is it that I'm adding the Items
to the ListBox
in a forach
that is taking so much time or is there anything that can be done to speed it up like ItemSource
which I tried but gave me error that ItemList should be empty before binding
I did try most of the answers for almost similar questions but none helped me.
Code
ResultsView.Items.Clear();
var redisClient = new RedisClient("localhost");
using (var client = redisClient.As<FileList>())
{
var foldersFromRedis = client.GetAll().Where(fileList => fileList.FileName.Contains(this.Search.Text.ToLower()));
foreach (FileList fileList in foldersFromRedis)
{
var listViewItem = new ListViewItem { Content = fileList.FileName , Tag = fileList.FolderName };
this.ResultsView.Items.Add(listViewItem);
}
}
//this.ResultsView.ItemsSource = ResultsFileList;
<ListBox Height="374" ScrollViewer.VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Margin="10,0,0,0" Name="ResultsView" VerticalAlignment="Bottom" Width="405" BorderThickness="0" SelectionChanged="MovieNameSelectionChanged" FontFamily="Nobile" FontSize="13" Background="#A6FCFCFC" Foreground="Black" FontStretch="Normal">
<GridView>
<GridViewColumn Header="FileName" DisplayMemberBinding="{Binding FileName}"/>
</GridView>
</ListBox>
UPDATE 1:
Added the ViewCollectionSource as below
private void ApplyViewCollectionSource()
{
_viewSource.Filter += ViewSourceFilter;
_viewSource.Source = _fileList = (List<FileList>)PopulateFileListEnglishWithReturn();
ResultsView.ItemsSource = _viewSource.View;
_timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
_timer.Tick += (o, e) =>
{
_timer.Stop();
_viewSource.View.Refresh();
};
Search.TextChanged += (o, e) => _timer.Start();
}
which is being called in the Constructor. The filter is as below:
private void ViewSourceFilter(object sender, FilterEventArgs e)
{
var src = e.Item as FileList;
e.Accepted = src != null;
if (string.IsNullOrEmpty(Search.Text)) return;
var regex = new Regex(Search.Text, RegexOptions.IgnoreCase);
e.Accepted = regex.IsMatch(src.FileName);
}
This works perfect for my requirement but only after the 3rd character in the TextBox. After typing the 1st char it takes 2 seconds for the ListBox
to get updated and the 2nd char takes 1 second. After this, its almost instant. There are about 5000 items in the master list _fileList
.
Any ways to improve the speed during the first 2 chars search ?