lweb/inc/lweb_file_cache.pbi

101 lines
No EOL
3.8 KiB
Text

;Buggy FileCache Routines ... Wiso auch immer das noch nicht richtig Funktioniert.
Procedure FileCacheCleaner(Information.i)
Protected.i Selected
Protected.i Counter
Protected.i MaxSize = Information
Repeat
Delay(1000) ;Nur Alle Sekunden Prüfen
Counter + 1
LockMutex(file_cache_mutex)
ResetMap(m_file_cache_map())
;Debug "FileCacheCleaner" + Str(Counter) + " Map Size:" + Str(MapSize(m_file_cache_map()))
If NextMapElement(m_file_cache_map())
Repeat
m_file_cache(MapKey(m_file_cache_map()))\Timer - 1
If m_file_cache(MapKey(m_file_cache_map()))\Timer <= 0
If m_file_cache(MapKey(m_file_cache_map()))\Buffer > 0
configuration\cache\current - m_file_cache(MapKey(m_file_cache_map()))\Size
FreeMemory(m_file_cache(MapKey(m_file_cache_map()))\Buffer)
DeleteMapElement(m_file_cache(), MapKey(m_file_cache_map()))
Debug "Info:["+Str(innercount)+"] Killed:["+MapKey(m_file_cache_map())+"]"
Selected = DeleteMapElement(m_file_cache_map())
Else
Debug "Info:["+Str(innercount)+"] Could Not kill:["+MapKey(m_file_cache_map())+"]"
EndIf
Else
Selected = NextMapElement(m_file_cache_map())
EndIf
Until Selected = 0
EndIf
UnlockMutex(file_cache_mutex)
ForEver
EndProcedure
Procedure FileCache(Information.i)
;http://purearea.net/pb/english/manual/reference/ug_memory.html
Protected FileCacheCleanerThread.i
Protected Tempbuffer.i, MaxSize.i, Current.i
MaxSize = configuration\cache\maxsize * 1024 * 1024
FileCacheCleanerThread = CreateThread(@FileCacheCleaner(), MaxSize)
Repeat
WaitSemaphore(file_cache_semaphore)
Debug "Adresse:"+m_file_cache()\Buffer
If (m_file_cache()\Size + configuration\cache\current) <= MaxSize
configuration\cache\current + m_file_cache()\Size
Tempbuffer = AllocateMemory(m_file_cache()\Size)
CopyMemory(m_file_cache()\Buffer, Tempbuffer, MemorySize(m_file_cache()\Buffer))
m_file_cache()\Buffer = Tempbuffer
m_file_cache()\Timer = configuration\cache\time
m_file_cache()\Is = #True
m_file_cache_map(MapKey(m_file_cache())) = #True
SignalSemaphore(file_cache_semaphore_thread)
Else
Debug "Cache Full"
SignalSemaphore(file_cache_semaphore_thread)
EndIf
ForEver
EndProcedure
Procedure AddFileToCache(MemoryID.i, FileName.s, Size.i)
LockMutex(file_cache_mutex)
Debug "Cache MaxSize:"+Str(configuration\cache\maxsize*1024*1024)+" Actual Size:"+Str(configuration\cache\current)
m_file_cache(FileName)\Buffer = MemoryID
m_file_cache(FileName)\Size = Size
SignalSemaphore(file_cache_semaphore)
WaitSemaphore(file_cache_semaphore_thread)
If m_file_cache(FileName)\Is
Debug FileName+" Size:"+Str(m_file_cache(FileName)\Size)+" Timer:"+Str(m_file_cache(FileName)\Timer)+ " new Memory ID:"+Str(m_file_cache(FileName)\Buffer)
Else
Debug "File not in cache was to full:"+FileName+ " MaxSize:"+Str(configuration\cache\maxsize)+" Actual Size:"+Str(configuration\cache\current/1024/1024)
EndIf
UnlockMutex(file_cache_mutex)
EndProcedure
Procedure.s GetFileFromCache(FileName.s, MemoryID.i)
Protected String.s
If TryLockMutex(file_cache_mutex)
If m_file_cache(FileName)\Is
MemoryID = ReAllocateMemory(MemoryID, m_file_cache(FileName)\Size)
CopyMemory(m_file_cache(FileName)\Buffer, MemoryID, m_file_cache(FileName)\Size)
String.s = Str(m_file_cache(FileName)\Size) + ":" + Str(MemoryID)
Else
String.s = #error_string
EndIf
UnlockMutex(file_cache_mutex)
Debug "Get from cache: "+FileName+">"+String+"-------------------------------------------------------------<"
ProcedureReturn String.s
Else
ProcedureReturn #error_string
EndIf
EndProcedure