readCountRepo.go 4.6 KB
Newer Older
T
tanggen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or  modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package repos

import (
	"fmt"
	"github.com/finogeeks/ligase/model/service"
	log "github.com/finogeeks/ligase/skunkworks/log"
	"sync"
	"time"
)

type ReadCountRepo struct {
	cache     service.Cache
	readCount *sync.Map
	hlCount   *sync.Map
	updated   *sync.Map
	delay     int
}

type UpdatedCountKey struct {
	RoomID string
	UserID string
}

func NewReadCountRepo(
	delay int,
) *ReadCountRepo {
	tls := new(ReadCountRepo)
	tls.readCount = new(sync.Map)
	tls.hlCount = new(sync.Map)
	tls.updated = new(sync.Map)

	tls.delay = delay
	tls.startFlush()
	return tls
}

func (tl *ReadCountRepo) startFlush() error {
	go func() {
		t := time.NewTimer(time.Millisecond * time.Duration(tl.delay))
		for {
			select {
			case <-t.C:
				tl.flush()
				t.Reset(time.Millisecond * time.Duration(tl.delay))
			}
		}
	}()

	return nil
}

func (tl *ReadCountRepo) SetCache(cache service.Cache) {
	tl.cache = cache
}

func (tl *ReadCountRepo) GetRoomReadCount(roomID, userID string) (int64, int64) {
	key := fmt.Sprintf("%s:%s", roomID, userID)
	readCount := int64(0)
	hlCount := int64(0)

	if val, ok := tl.readCount.Load(key); ok {
		readCount = val.(int64)
	} else {
		tl.UpdateRoomReadCountFromCache(roomID, userID)
		if val, ok := tl.readCount.Load(key); ok {
			readCount = val.(int64)
		}
	}

	if val, ok := tl.hlCount.Load(key); ok {
		hlCount = val.(int64)
	} else {
		tl.UpdateRoomReadCountFromCache(roomID, userID)
		if val, ok := tl.hlCount.Load(key); ok {
			hlCount = val.(int64)
		}
	}

	return readCount, hlCount
}

func (tl *ReadCountRepo) UpdateRoomReadCountFromCache(roomID, userID string) {
	key := fmt.Sprintf("%s:%s", roomID, userID)
	hl, count, _ := tl.cache.GetRoomUnreadCount(userID, roomID)
	tl.readCount.Store(key, count)
	tl.hlCount.Store(key, hl)
}

func (tl *ReadCountRepo) UpdateRoomReadCount(roomID, userID, updateType string) {
	key := fmt.Sprintf("%s:%s", roomID, userID)

	switch updateType {
	case "reset":
		tl.readCount.Store(key, int64(0))
		tl.hlCount.Store(key, int64(0))
	case "increase":
		if val, ok := tl.readCount.Load(key); ok {
			tl.readCount.Store(key, val.(int64)+1)
		} else {
			tl.UpdateRoomReadCountFromCache(roomID, userID)
			if val, ok := tl.readCount.Load(key); ok {
				tl.readCount.Store(key, val.(int64)+1)
			} else {
				tl.readCount.Store(key, int64(1))
			}
		}
	case "increase_hl":
		if val, ok := tl.hlCount.Load(key); ok {
			tl.hlCount.Store(key, val.(int64)+1)
		} else {
			tl.UpdateRoomReadCountFromCache(roomID, userID)
			if val, ok := tl.hlCount.Load(key); ok {
				tl.hlCount.Store(key, val.(int64)+1)
			} else {
				tl.hlCount.Store(key, int64(1))
			}
		}
	case "decrease":
		if val, ok := tl.readCount.Load(key); ok {
			count := val.(int64)
			if count > 0 {
				tl.readCount.Store(key, count-1)
				if val1, ok := tl.hlCount.Load(key); ok {
					hlCount := val1.(int64)
					if hlCount >= count {
						tl.hlCount.Store(key, count-1)
					}
				}
			}

		} else {
			tl.UpdateRoomReadCountFromCache(roomID, userID)
			if val, ok := tl.readCount.Load(key); ok {
				count := val.(int64)
				if count > 0 {
					tl.readCount.Store(key, count-1)
					if val1, ok := tl.hlCount.Load(key); ok {
						hlCount := val1.(int64)
						if hlCount >= count {
							tl.hlCount.Store(key, count-1)
						}
					}
				}
			} else {
				tl.readCount.Store(key, int64(0))
				tl.hlCount.Store(key, int64(0))
			}
		}
	}

	upKey := UpdatedCountKey{
		RoomID: roomID,
		UserID: userID,
	}
	tl.updated.Store(key, &upKey)
}

func (tl *ReadCountRepo) flush() {
	log.Infof("ReadCountRepo start flush")
	tl.updated.Range(func(key, value interface{}) bool {
		tl.updated.Delete(key)

		upKey := value.(*UpdatedCountKey)
		readCount, hlCount := tl.GetRoomReadCount(upKey.RoomID, upKey.UserID)
		err := tl.cache.SetRoomUnreadCount(upKey.UserID, upKey.RoomID, readCount, hlCount)
		if err != nil {
			log.Errorf("ReadCountRepo write cache roomID %s userID %s err %v", upKey.RoomID, upKey.UserID, err)
		}

		return true
	})
	log.Infof("ReadCountRepo finished flush")
}