Beispiel #1
0
        public virtual async Task UpdateAsync(T record)
        {
            var oldRecord = await FindByIdAsync(record.Id);

            _dbContext.Set <T>().AddOrUpdate(record);
            await _dbContext.SaveChangesAsync();

            //Set Post Tags & Categories
            await UpdatePostTagsAsync(record.Id, record.Tags?.Select(p => p.Tag).ToList());
            await UpdatePostCategoriesAsync(record.Id, record.Categories?.Select(p => p.Id).ToList());

            QueryCacheManager.ExpireTag(_cacheKey);
            MethodCache.ExpireTag(_cacheKey);

            _eventPublisher.EntityUpdated(record, oldRecord);
        }
Beispiel #2
0
        public virtual async Task <int> AddAsync(T record)
        {
            var postTags       = record.Tags?.Select(p => p.Tag).ToList();
            var postCategories = record.Categories?.Select(p => p.Id).ToList();

            record.Tags       = null;
            record.Categories = null;

            _dbContext.Set <T>().Add(record);
            await _dbContext.SaveChangesAsync();

            //Set Post Tags & Categories
            await UpdatePostTagsAsync(record.Id, postTags);
            await UpdatePostCategoriesAsync(record.Id, postCategories);

            QueryCacheManager.ExpireTag(_cacheKey);
            MethodCache.ExpireTag(_cacheKey);

            _eventPublisher.EntityInserted(record);

            return(record.Id);
        }
Beispiel #3
0
        public virtual async Task DeleteAsync(int id)
        {
            var record = await FindByIdAsync(id);

            await _userLikesService.DeletePostLikesAsync(id);

            await _userWishlistService.DeletePostFromWishlistAsync(id);

            await _dbContext.PostComments.Where(p => p.PostId == id).DeleteAsync();

            var post = await FindByIdAsync(id);

            _dbContext.Set <T>().Remove(post);
            await _dbContext.SaveChangesAsync();

            await _localizedEntityService.DeleteEntityAllLocalizedStringsAsync(typeof(T).Name, id);

            QueryCacheManager.ExpireTag(_cacheKey);
            MethodCache.ExpireTag(_cacheKey);

            _eventPublisher.EntityDeleted(record);
        }
Beispiel #4
0
 protected virtual void ClearCache()
 {
     QueryCacheManager.ExpireTag(CacheTags.Country);
     MethodCache.ExpireTag(CacheTags.Country);
 }
Beispiel #5
0
        public virtual long CreateIndex()
        {
            try
            {
                var watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                if (System.IO.Directory.Exists(_indexFilesPath))
                {
                    try
                    {
                        using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                            directory.ClearLock(directory.GetLockId());
                    }
                    catch
                    {
                    }

                    FileUtils.DeleteDirRecursively(new DirectoryInfo(_indexFilesPath));
                }

                System.IO.Directory.CreateDirectory(_indexFilesPath);

                if (System.IO.Directory.Exists(_spellFilesPath))
                {
                    try
                    {
                        using (var directory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath)))
                            directory.ClearLock(directory.GetLockId());
                    }
                    catch
                    {
                    }

                    FileUtils.DeleteDirRecursively(new DirectoryInfo(_spellFilesPath));
                }

                System.IO.Directory.CreateDirectory(_spellFilesPath);


                var allPosts = _postService.GetAsQueryable().Where(p => p.Published)
                               .Include(p => p.Descriptions)
                               .Include(p => p.Tags)
                               .Include(p => p.Categories);
                var languages = _languagesService.GetAsEnumerable();

                var analyzer = new StandardAnalyzer(Version);
                using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                {
                    using (var writer =
                               new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        foreach (var post in allPosts)
                        {
                            writer.AddDocument(MapPost(post, post.PostType));//Default values

                            foreach (var language in languages.OrderByDescending(p => p.IsDefault))
                            {
                                var localizedMap = MapPost(post, language, post.PostType);
                                if (localizedMap != null)
                                {
                                    writer.AddDocument(localizedMap);    //Localized values
                                }
                            }
                        }

                        writer.Optimize();
                        writer.Commit();
                    }
                }

                using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                {
                    using (var spellDirectory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath)))
                    {
                        using (var indexReader = IndexReader.Open(directory, readOnly: true))
                        {
                            using (var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(spellDirectory))
                            {
                                // Create SpellChecker Index
                                spellChecker.ClearIndex();
                                spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Title"));
                                spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Description"));

                                spellChecker.Close();
                            }
                        }
                    }
                }

                watch.Stop();

                MethodCache.ExpireTag(CacheTags.Search);

                return(watch.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                MethodCache.ExpireTag(CacheTags.Search);
                _eventPublisher.Publish(new CreateSearchIndexesFailEvent(e));
                throw;
            }
        }