-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.go
More file actions
74 lines (61 loc) · 1.91 KB
/
proxy.go
File metadata and controls
74 lines (61 loc) · 1.91 KB
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
package proxy
import "fmt"
type User struct {
ID int32
}
type UserFinder interface {
FindUser(id string) (User, error)
}
type UserList []User
//FindUser will iterate over the list to try to find a user with the same
//name that the param or return an error if it can't find it
func (t *UserList) FindUser(id int32) (User, error) {
for i := 0; i < len(*t); i++ {
if (*t)[i].ID == id {
return (*t)[i], nil
}
}
return User{}, fmt.Errorf("Could not find user with id: %d", id)
}
//AddUser adds a new user to the end of the Users slice
func (t *UserList) addUser(newUser User) {
*t = append(*t, newUser)
}
type UserListProxy struct {
MockedDatabase *UserList
StackCache UserList
StackSize int
LastSearchUsedCache bool
}
//addUserToStack takes the user argument and adds it to the stack in place.
//If the stack is full it removes the first element on it before adding.
func (u *UserListProxy) addUserToStack(user User) {
if len(u.StackCache) >= u.StackSize {
u.StackCache = append(u.StackCache[1:], user)
} else {
u.StackCache.addUser(user)
}
}
//FindUser will search for the specified name in the parameter in the cache
//list. If it finds it, it will return it. If not, it will search in the heavy
//list. Finally, if it's not in the heavy list, it will return an error
//(generated from the heavy list)
func (u *UserListProxy) FindUser(id int32) (User, error) {
//Search for the object in the cache list first
user, err := u.StackCache.FindUser(id)
if err == nil {
fmt.Println("Returning user from cache")
u.LastSearchUsedCache = true
return user, nil
}
//Object is not in the cache list. Search in the heavy list
user, err = u.MockedDatabase.FindUser(id)
if err != nil {
return User{}, err
}
//Adds the new user to the stack, removing the last if necessary
u.addUserToStack(user)
fmt.Println("Returning user from database")
u.LastSearchUsedCache = false
return user, nil
}