mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-30 22:12:25 -05:00 
			
		
		
		
	i have no idea what i'm doing
This commit is contained in:
		
					parent
					
						
							
								34915b3e66
							
						
					
				
			
			
				commit
				
					
						d26c689696
					
				
			
		
					 3 changed files with 152 additions and 63 deletions
				
			
		
							
								
								
									
										56
									
								
								internal/processing/timeline/sharedcache.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								internal/processing/timeline/sharedcache.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,56 @@ | |||
| package timeline | ||||
| 
 | ||||
| import ( | ||||
| 	"sort" | ||||
| 	"sync" | ||||
| ) | ||||
| 
 | ||||
| type sharedCache struct { | ||||
| 	data      map[string]*post | ||||
| 	maxLength int | ||||
| 	*sync.Mutex | ||||
| } | ||||
| 
 | ||||
| func newSharedCache(maxLength int) *sharedCache { | ||||
| 	return &sharedCache{ | ||||
| 		data:      make(map[string]*post), | ||||
| 		maxLength: maxLength, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (s *sharedCache) shrink() { | ||||
| 	// check if the length is longer than max size | ||||
| 	toRemove := len(s.data) - s.maxLength | ||||
| 
 | ||||
| 	if toRemove > 0 { | ||||
| 		// we have stuff to remove so lock the map while we work | ||||
| 		s.Lock() | ||||
| 		defer s.Unlock() | ||||
| 
 | ||||
| 		// we need to time-sort the map to remove the oldest entries | ||||
| 		// the below code gives us a slice of keys, arranged from newest to oldest | ||||
| 		postSlice := make([]*post, 0, len(s.data)) | ||||
| 		for _, v := range s.data { | ||||
| 			postSlice = append(postSlice, v) | ||||
| 		} | ||||
| 		sort.Slice(postSlice, func(i int, j int) bool { | ||||
| 			return postSlice[i].createdAt.After(postSlice[j].createdAt) | ||||
| 		}) | ||||
| 
 | ||||
| 		// now for each entry we have to remove, delete the entry from the map by its status ID | ||||
| 		for i := 0; i < toRemove; i = i + 1 { | ||||
| 			statusID := postSlice[i].statusID | ||||
| 			delete(s.data, statusID) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (s *sharedCache) put(post *post) { | ||||
| 	s.Lock() | ||||
| 	defer s.Unlock() | ||||
| 	s.data[post.statusID] = post | ||||
| } | ||||
| 
 | ||||
| func (s *sharedCache) get(statusID string) *post { | ||||
| 	return s.data[statusID] | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue