-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(conc): introduce conc operations with chans and mutex for queue (#7
- Loading branch information
Showing
6 changed files
with
198 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,42 @@ | ||
#### memcache | ||
### memcache | ||
|
||
in memory cache via http api | ||
|
||
#### endpoints | ||
|
||
`/save` | ||
|
||
to save data in to cache | ||
data format | ||
|
||
```json | ||
{ | ||
"key":"some-key", | ||
"value":"some-value" | ||
} | ||
``` | ||
|
||
req type - **POST** | ||
|
||
`/get` | ||
|
||
to retrieve data from cache | ||
|
||
data format | ||
|
||
```json | ||
{ | ||
"key":"some-key" | ||
} | ||
``` | ||
|
||
#### about | ||
|
||
this is a concurrent implementation of cache via http. | ||
Can be used across any service layer as a stage or landing for some data | ||
which you require to consume / refer / lookup | ||
|
||
**TODO** | ||
|
||
- [ ] benchmark and test for disaster recovery | ||
- [ ] implement a leader process to have data sync between instances |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,65 @@ | ||
package datastructure | ||
|
||
// type inMemoryQueue []any | ||
import "sync" | ||
|
||
type queue struct { | ||
list []any | ||
lastAdded int | ||
lock *sync.RWMutex | ||
} | ||
|
||
func new(size int) *queue { | ||
return &queue{ | ||
list: make([]any, size), | ||
lastAdded: 0, | ||
lock: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (q *queue) add(k any) { | ||
q.lock.Lock() | ||
defer q.lock.Unlock() | ||
q.list[q.lastAdded] = k | ||
q.lastAdded++ | ||
} | ||
|
||
func (q *queue) swap(idx int) { | ||
q.lock.Lock() | ||
defer q.lock.Unlock() | ||
q.list[0], q.list[idx] = q.list[idx], q.list[0] | ||
} | ||
|
||
func (q *queue) getIdx(k any) int { | ||
totLen := len(q.list) - 1 | ||
for i := 0; i < totLen; i++ { | ||
if q.list[i] == k { | ||
return i | ||
} | ||
func (q *queue) getIdx(k any) chan int { | ||
out := make(chan int) | ||
go func() { | ||
totLen := len(q.list) - 1 | ||
for i := 0; i < totLen; i++ { | ||
if q.list[i] == k { | ||
out <- i | ||
} | ||
|
||
} | ||
return -1 | ||
} | ||
out <- -1 | ||
}() | ||
return out | ||
|
||
} | ||
|
||
var inMemoryIdx *queue | ||
|
||
func (q *queue) evict() { | ||
q.lock.Lock() | ||
defer q.lock.Unlock() | ||
lastElementPosition := len(q.list) - 1 | ||
q.list[lastElementPosition] = nil | ||
} | ||
|
||
// swap replaces first value in queue with | ||
// recently accessed index | ||
// func (idx *inMemoryQueue) swap(i int) { | ||
// tmp := (*idx)[0] | ||
// (*idx)[0] = (*idx)[i] | ||
// (*idx)[i] = tmp | ||
// } | ||
// | ||
// func (idx *inMemoryQueue) getIndex(k any) int { | ||
// totLen := len(*idx) - 1 | ||
// for i := 0; i < totLen; i++ { | ||
// if (*idx)[i] == k { | ||
// return i | ||
// } | ||
// } | ||
// return -1 | ||
// } | ||
|
||
// add inserts the key to last empty position in queue | ||
// func (idx *inMemoryQueue) add(k any) { | ||
// index := idx.check() | ||
// (*idx)[index] = k | ||
// } | ||
|
||
// func (idx *inMemoryQueue) check() int { | ||
// // when queue is empty return first position | ||
// if (*idx)[0] == nil { | ||
// return 0 | ||
// } | ||
// // get len of queue | ||
// totLen := len(*idx) - 1 | ||
// // return the first free space in queue | ||
// for i := totLen; i >= 0; i-- { | ||
// if (*idx)[i] == nil { | ||
// return i | ||
// } | ||
// } | ||
// return totLen | ||
// | ||
// } | ||
func (q *queue) removeAt(idx int) { | ||
q.lock.Lock() | ||
defer q.lock.Unlock() | ||
q.list = append(q.list[:idx], q.list[idx+1:]...) | ||
if q.lastAdded >= 1 { | ||
q.lastAdded = q.lastAdded - 1 | ||
} | ||
|
||
// evict follows last accessed/final element | ||
// in queue and remove it from queue and cache store | ||
// this is called only when queue is full and no space a vailable | ||
// func (idx *) evict() { | ||
// totLen := len(*idx) - 1 | ||
// inMemoryIdx[totLen] = nil | ||
// } | ||
} |
Oops, something went wrong.