Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:48:13+00:00 2026-06-15T07:48:13+00:00

I will have an activity feed per user that displays all activity related to

  • 0

I will have an activity feed per user that displays all activity related to events the user is subscribed to and the feed will pull in the most current 20 or so activities. The way I have it setup is all activity regardless of the event its related to is stored in one collection and the document itself has an “event” property I query against and index. The basic query is just select activities from collection where event is in the users event subscription list ordered by date. I store a hash of the list of the users event subscriptions and cache the results of the query using the hash as the key for xx seconds so if another user is subscribed to same exact events I can pull the results from cache instead, I’m not concerned with results being xx seconds stale.

Edit: added model and query example

Models:

User
{
    // useless properties excluded
    // fixed: hashset not list, can be quite large
    HashSet<string> Subscriptions { get; set; }
    string SubscriptionHash { get; set; } // precomputed hash of all strings in subscriptions
}

Activity
{
    // Useless properties excluded
    string ActivityType { get; set; }
}

Query:

if (cache[user.SubscriptionHash] != null)
    results = (HashSet<Activity>)cache[user.SubscriptionHash];
else
    results = session.Query<Activity>().Where(user.Subscriptions.Contains(e => e.ActivityType)).Take(20).ToList();

// add results to cache

My concern is if this is the BEST APPROACH to handle this or if there’s better ravendb voodoo to use. The single collection could grow into the millions if there’s alot of activities and I could potentially be storing thousands of keys in the cache when there’s thousands of users with endless combinations of subscription lists. These feeds are on the users landing page so it gets hit alot and I don’t want to just throw more hardware at the problem.

So answer im really looking for is if this is the best query to use or if there’s a better way to do it in Raven when I could be querying against millions of documents using list.Contains.

This is an asp.net 4.5 mvc 4 project using ravendb.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:48:14+00:00Added an answer on June 15, 2026 at 7:48 am

    Now here is how I would approach it. This is based on RaccoonBlog PostComments

    I would store each users events in a separate document (i.e. UserEvent in the example below) with the user having an additional property linking to it along with a number of events and a timestamp of the last event associated with the user. This would keep the user document much smaller but having alot of the important information

    In UserEvent it would be a simple document holding id, link to the userid this document references, a “event” collection, and a lasteventid. This way each “event” becomes a sub document for maintenance if needed.

    Lastly a Index on UserEvent that allows you to query the data easily

    public class User
    {
        public string Id { get; set; }
        // other user properties
    
        public string UserEventId { get; set; }
        public int NumberOfEvents { get; set; }
        public DateTimeOffset LastEvent { get; set; }
    }
    
    public class UserEvent
    {
        public string Id { get; set; }
    
        public string UserId { get; set; }
    
        public int LastEventId { get; set; }
    
        public ICollection<Event> Events { get; protected set; }
    
        public int GenerateEventId()
        {
            return ++LastEventId;
        }
    
        public class Event
        {
            public int Id { get; set; }
            public DateTimeOffset CreatedAt { get; set; }
            public string ActivityType { get; set; }
            // other event properties
        }
    }
    
    public class UserEvents_CreationDate : AbstractIndexCreationTask<UserEvent, UserEvents_CreationDate.ReduceResult>
    {
        public UserEvents_CreationDate()
        {
            Map = userEvents => from userEvent in userEvents
                                from evt in userEvent.Events
                                select new
                                {
                                    evt.CreatedAt,
                                    EventId = evt.Id,
                                    UserEventId = userEvent.Id,
                                    userEvent.UserId,
                                    evt.ActivityType
                                };
            Store(x => x.CreatedAt, FieldStorage.Yes);
            Store(x => x.EventId, FieldStorage.Yes);
            Store(x => x.UserEventId, FieldStorage.Yes);
            Store(x => x.UserId, FieldStorage.Yes);
            Store(x => x.ActivityType, FieldStorage.Yes);
        }
    
        public class ReduceResult
        {
            public DateTimeOffset CreatedAt { get; set; }
            public int EventId { get; set; }
            public string UserEventId { get; set; }
            public string UserId { get; set; }
            public string ActivityType { get; set; }
        }
    }
    
    public static class Helpers
    {
        public static DateTimeOffset AsMinutes(this DateTimeOffset self)
        {
            return new DateTimeOffset(self.Year, self.Month, self.Day, self.Hour, self.Minute, 0, 0, self.Offset);
        }
    
        public static IList<Tuple<UserEvents_CreationDate.ReduceResult, User>> QueryForRecentEvents(
            this IDocumentSession documentSession,
            Func
                <IRavenQueryable<UserEvents_CreationDate.ReduceResult>, IQueryable<UserEvents_CreationDate.ReduceResult>
                > processQuery)
        {
            IRavenQueryable<UserEvents_CreationDate.ReduceResult> query = documentSession
                .Query<UserEvents_CreationDate.ReduceResult, UserEvents_CreationDate>()
                .Include(comment => comment.UserEventId)
                .Include(comment => comment.UserId)
                .OrderByDescending(x => x.CreatedAt)
                .Where(x => x.CreatedAt < DateTimeOffset.Now.AsMinutes())
                .AsProjection<UserEvents_CreationDate.ReduceResult>();
    
            List<UserEvents_CreationDate.ReduceResult> list = processQuery(query).ToList();
    
            return (from identifier in list
                    let user = documentSession.Load<User>(identifier.UserId)
                    select Tuple.Create(identifier, user))
                .ToList();
        }
    }
    

    Then all you have to do to query is something like.

     documentSession.QueryForRecentEvents(q => q.Where(x => x.UserId == user.Id &&  x.ActivityType == "asfd").Take(20)).Select(x => x.Item1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an Activity that will behave like a popup menu is actually
I have a table that potentially will have high number of inserts per second,
right to business. I have an activity feed which gets all different kinds of
I have an activity that starts on demand of the user. The user can
I have activity that parses XML feed (with news) and loads parsed data (text
I have problem understanding shared preferences. I have activity where user will insert password,
I have an application and every new created activity will start an async task
My application have a service and an activity. Sometimes the service will send a
I have a main app that will have many plugins. Here is the PluginReader
I'm making an app that uses a video feed. There will be several UI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.