madrang / ts3Ext

TeamSpeak 3 Python Extensions
GNU General Public License v3.0
1 stars 1 forks source link

Global cache #2

Open Bluscream opened 6 years ago

Bluscream commented 6 years ago

Its so sad that the pluginapi cant access teamspeaks bookkeeping and everything has to be requested over and over again, i think it would be superawesome if ts3ext could act as cache for all plugins. So it starts as own plugin and caches new events and other plugins dont have to create new instances of ts3ext but instead access the already existing one, that would improve perfomance by a lot

Sent from my TAB 2 A10 using FastHub

Bluscream commented 6 years ago

Also it would be neat if ts3ext would cache clients longer than theyre online, teamspeak discards clientids for reuse after 10 mins so caching them for 5 mins shouldnt be a problem if they are only cached once. Also i wou!d love to have props like server.last_joined, channel.last_joined channel.last_talk_power_granted, channel.creator

madrang commented 6 years ago

I try to not do memory management or even keep a strong reference to anything. Maybe in a different module ? But if you want a reference with all cached information to stay alive just keep a reference in your code somewhere.

Here i do something similar to keep the client id and name in memory for a talking user. https://github.com/madrang/ts3Ext/blob/master/__init__.py#L442 Add and remove from a list as needed and it will keep all possible information if requested once. But remove that reference and everything will be freed.

Edit, it should be easy to extend the objects for them to have any other properties. https://stackoverflow.com/questions/3464061/cast-base-class-to-derived-class-python-or-more-pythonic-way-of-extending-class/3464154#3464154

Bluscream commented 6 years ago

But if you want a reference with all cached information to stay alive just keep a reference in your code somewhere.

But creating a new ts3SessionHost() every time would become unperformant when many scripts use it :/

Bluscream commented 6 years ago

I tried my best to create something that i can use for testing:

https://github.com/Bluscream/pyTSon_plugins/blob/master/scripts/aaa_ts3Ext/__init__.py https://github.com/Bluscream/pyTSon_plugins/blob/master/include/autorun.py#L139

One question, how do i iterate over "generator" objects like server.users?

>>> server.users

<generator object users at 0x0000021BB189CBA0>

EDIT: got it

>>> for user in server.users:

...     print(user.displayName)
... 

F R A M Ξ R Λ D I O

KissFM

I ♥ TOP 100
ModiX

Bluscream

Traceback (most recent call last):
  File "C:/Users/blusc/AppData/Roaming/TS3Client/plugins/pyTSon/include\pytsonui\console.py", line 355, in runCommand
    exec(cmd, self.globals)
  File "<string>", line 2, in <module>
  File "C:/Users/blusc/AppData/Roaming/TS3Client/plugins/pyTSon/include\ts3Ext\__init__.py", line 706, in displayName
    if err != ts3defines.ERROR_ok: raise ts3Error("Error getting client display name: (%s, %s)" % (err, ts3lib.getErrorMessage(err)[1]))
ts3Ext.ts3Error: Error getting client display name: (512, invalid clientID)

EDIT 2: Why aren't they just a list of ts3User objects? That would allow stuff like print(server.users)

madrang commented 6 years ago

You do not have to modify the session host and should create only one when you init your plugin and keep your reference to it alive.

I use WeakValueDictionary to store users objects. https://docs.python.org/2/library/weakref.html#weakref.WeakValueDictionary

Like i said, to force that information to stay in memory, all you need is to keep a strong reference to your user object and then the values will stay cached. https://github.com/madrang/ts3Ext/blob/master/__init__.py#L442

"A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. A primary use for weak references is to implement caches or mappings holding large objects, where it’s desired that a large object not be kept alive solely because it appears in a cache or mapping."

IE: Keeping the user object in a normal list, will stop it from being garbage collected.

Bluscream commented 6 years ago

Ahh okay thanks for that info. i'd like to cache users after they're disconnected tho is there a way to add that to ts3Ext?